Mongo isn't expiring old collections. I checked to make sure my index is type date.
var keys = IndexKeys.Ascending("expiry");
var options = IndexOptions.SetTimeToLive(TimeSpan.FromMinutes(1));
collection.EnsureIndex(keys, options);
this.ExpireDate = new BsonDateTime(DateTime.UtcNow.AddMinutes(5));
var insertResult = collection.Insert(this);
Any tips would be gladly appreciated.
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "Showsv1.ShowInfo",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"expiry" : 1
},
"ns" : "Showsv1.ShowInfo",
"name" : "expiry_1",
"expireAfterSeconds" : 60
}
]
"expiry" : ISODate("2013-02-15T02:40:45.876Z")
The code was missing [BsonElement("expiry")] on top of the ExpireTime property.
Thanks #WiredPrairie for the tip.
Related
As described here from MongoDB 4.2 on it is possible to update documents with an aggregation pipeline.
It means that now it is possible to express "conditional updates based on current field values or updating one field using the value of another field(s)".
For instance:
db.members.update(
{ },
[
{ $set: { status: "Modified", comments: [ "$misc1", "$misc2" ], lastUpdate: "$$NOW" } },
{ $unset: [ "misc1", "misc2" ] }
],
{ multi: true }
)
My question is: how can I do this using MongoDB on C#?
IMongoCollection's UpdateMany takes UpdateDefinition<T> as second parameter and PipelineUpdateDefinition is one of the derived classes. There's no expression trees support so far but you can utilize BsonDocument class:
IMongoCollection<BsonDocument> col = ...;
var pipeline = new EmptyPipelineDefinition<BsonDocument>()
.AppendStage("{ $addFields : { " +
"status : 'Modified'," +
"comments: [ '$misc1', '$misc2' ]," +
"lastUpdate: '$$NOW' " +
"} }",
BsonDocumentSerializer.Instance)
.AppendStage("{ $project : { 'misc1':0, 'misc2':0 } }",
BsonDocumentSerializer.Instance);
col.UpdateMany(new BsonDocument(), pipeline);
which executes following command (trace from MongoDB driver):
"updates" : [
{
"q" : { },
"u" : [
{ "$addFields" : { "status" : "Modified", "comments" : ["$misc1", "$misc2"], "lastUpdate" : "$$NOW" } },
{ "$project" : { "misc1" : 0, "misc2" : 0 } }],
"multi" : true }
}
]
I got some freeze in update documents in mongoDb through C#. What i need to do to fix it?
I have MongoDb collection with ~230000 documents. Signature is
{
"_id" : ObjectId("5c5d4c3a086dad5e5ab8d397"),
"Uid" : "SomeiDString",
"SomeTextField" : "SomeText"
}
Indexes - two fields:
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "db.mycollection"
},
{
"v" : 2,
"unique" : true,
"key" : {
"Uid" : 1.0
},
"name" : "Uid_1",
"ns" : "db.mycollection",
"background" : true
}
In C# I make async operation UpdateOneAsync, with filter on Uid and update SomeTextField.
private async Task <bool> PutSomeTextMongoDb(string Uid, string SomeText) {
var collection = database.GetCollection<User>(MONGO_DB_COLLEŠ”TION);
var filter = Builders<User>.Filter.Eq("Uid", Uid);
var update = Builders<User>.Update.Set("SomeText", SomeText);
var option = new UpdateOptions {IsUpsert = true};
var result = await collection.UpdateOneAsync(filter, update, option);
return result.ModifiedCount != 0;
}
Problem that I have: 5000 request got good avr time. Is about 2-3 ms. But some of them grow up to ~800-1000 ms... I know, it's only 5-6 long operation from 5k, but what is it? May be some locks for update operation, or when Mongo flush data i got some queue? mongostat and mongotop didn't answered me on this question...
Is it possible to list all the restaurants and their latest grade, if grades is a an array within a restaurant?
{
"_id" : ObjectId("56bf7957b5e096fd06b755b2"),
"grades" : [
{
"date" : ISODate("2014-11-15T00:00:00.000Z"),
"grade" : "Z",
"score" : 38
},
{
"date" : ISODate("2014-05-02T00:00:00.000Z"),
"grade" : "A",
"score" : 10
},
{
"date" : ISODate("2013-03-02T00:00:00.000Z"),
"grade" : "A",
"score" : 7
},
{
"date" : ISODate("2012-02-10T00:00:00.000Z"),
"grade" : "A",
"score" : 13
}
],
"name" : "Brunos On The Boulevard",
}
I would want to get:
{
"_id" : ObjectId("56bf7957b5e096fd06b755b2"),
"grades" : [
{
"date" : ISODate("2014-11-15T00:00:00.000Z"),
"grade" : "Z",
"score" : 38
}
],
"name" : "Brunos On The Boulevard",
}
Explanation
The answer below uses the unwind operator. There's a really simple explanation of it on this answer, should anyone be confused by it as I was.
An option could be doing an aggregate with two operations, an Unwind which deconstructs your array field from the input documents to output a document for each element, and later a sort operation by date in descending order. This way you can get the result you are expected selecting the first element from the aggregate result:
var result = collection.Aggregate()
.Unwind(e => e["grades"])
.SortByDescending(e=>e["grades.date"])
.FirstOrDefault();
we have a problem with our indexes. We have an index on our emails but it throws errors like such:
> db.User.insert({email: "hell33o#gmail.com", "_id" : BinData(3,"iKyq6FvBCdd54TdxxX0JhA==")})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error index: placetobe.User.$email_text dup key: { : \"com\", : 0.6666666666666666 }"
}
})
when we have the index created with our C# driver like this
Created by C# with:
CreateIndexOptions options = new CreateIndexOptions {Unique = true};
_collection.Indexes.CreateOneAsync(Builders<User>.IndexKeys.Text(_ => _.email), options);
resulted in
{
"v" : 1,
"unique" : true,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "email_text",
"ns" : "placetobe.User",
"weights" : {
"email" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 2
}
but if we create it with the MongoDB console like this it works:
{
"v" : 1,
"unique" : true,
"key" : {
"email" : 1
},
"name" : "email_1",
"ns" : "placetobe.User"
}
I don't understand the difference between the two indexes, but they have an effect on our DB. We also have problems with a Collectin that saves names. we get duplicate exceptions on "Molly" if we try to insert "Molli". With the emails is seems to give us errors whenever we have two "gmail" emails in the collection or two ".com" emails etc.
This is a University project and we have to turn it in tomorrow. we're really in trouble, help would be much appreciated
You don't want your email to be a text Index. Text indices allow you to search large amounts of text in MongoDB like if you were parsing through comments or something. All you want is to make sure your emails aren't duplicated so you should use an ascending or descending index.
CreateIndexOptions options = new CreateIndexOptions {Unique = true};
_collection.Indexes.CreateOneAsync(Builders<User>.IndexKeys.Ascending(_ => _.email), options)
I am battling strange behavior of index creation from hours. I am trying to re-build my sample datas so i drop my collection before insert new datas and before insert new datas i create indexes again like below.
db.GetCollection("Posts").EnsureIndex("Name","Title","Owner");
After that i am trying to Execute sorted Query but MongoDb throws exception and says that
QueryFailure flag was too much data for sort() with no index. add an index or specify a smaller limit
But if i put this line code db.GetCollection("Post").EnsureIndex("Name"); before execute query, it works without problem. Then i have realized that if i use this before rebuild datas it works. There should to be a bug in overloading method or something i have missed.
I am using 10Gen .net driver version 1.2 and i have checked which indexes exist before execure query. Here it is
db.GetCollection("Posts").EnsureIndex("Name","Title","Owner");
db.GetIndexes();//result
[0]: { "v" : 1, "key" : { "_id" : 1 }, "ns" : "Posts", "name" : "_id_" }
[1]: { "v" : 1, "key" : { "Name" : 1, "Title" : 1, "Owner" : 1 }, "ns" : "Posts", "name" : "Name_1_Title_1_Owner_1_" }
db.GetCollection("Posts").EnsureIndex("Title") // i call this for other indexes too
db.GetIndexes();
[0]: { "v" : 1, "key" : { "_id" : 1 }, "ns" : "Posts", "name" : "_id_" }
[1]: { "v" : 1, "key" : { "Name" : 1 }, "ns" : "Posts", "name" : "Name_1" }
[2]: { "v" : 1, "key" : { "Title" : 1 }, "ns" : "Posts", "name" : "Title_1" }
[4]: { "v" : 1, "key" : { "Owner" : 1 }, "ns" : "Posts", "name" : "Owner_1" }
I can't tell from your example exactly what you think isn't working.
One thing to keep in mind is that EnsureIndex only knows what's going on within your own process. So if you remove an index or drop a collection using the mongo shell EnsureIndex won't pick up on that. You can use CreateIndex instead of EnsureIndex if you want to make sure the index exists regardless of what other processes might have done in the meantime.
Let me know if you can provide more details on how to reproduce what you are seeing.