I have some document stored in MongoDB (2.4.9) with and array if documents field:
{
"_id" : "some id",
"class" : "somevalue",
...
"externalAlarmDefinition" : [
{
"idx" : 1,
"inputId" : 1
},
{
"idx" : 2,
"inputId" : 2
},
...
{
"idx" : 6,
"inputId" : 7
}
]
}
For some reason, when I query this object I get BsonElement who's value is a BsonArray with one element - that element in turn is another BsonArray which contains the actual BsonDocuments. See image for the structure:
Does this make sense? I expected that the value of the BsonElement would be a BsonArray with the 6 BsonDocuments.
Am I missing something - can someone explain this?
I am using Mongo Driver 1.9.1.221 that I got using nuget
Related
I have the following collection
{
"_id" : ObjectId("57315ba4846dd82425ca2408"),
"myarray" : [
{
userId : ObjectId("570ca5e48dbe673802c2d035"),
point : 5
},
{
userId : ObjectId("613ca5e48dbe673802c2d521"),
point : 2
},
]}
and I want to add a new element to this array in C#, Could please to help me I try to do it's by $push but always give me this error. thanks
MongoDB.Driver.AddToSetUpdateDefinition<TDocument, TItem>.Render(IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)`enter code here`
I want to update or insert into to mongo collection "Member". Under this collection i have an array MagazineSubscription. Here magazine Code is unique. Please refer the sample JSON.
So if need to update or insert into mongo using C# mongo driver.
First I need to check this code exist
2, If it exist update one
If it does not exist insert.
Is there any way I can do in one step. Like if it already exist update otherwise insert. Instead of hit twice. Because my collection is very big.
{
"_id" : ObjectId("5c44f7017en0893524d4e9b1"),
"Code" : "WH01",
"Name" : "Lara",
"LastName" : "John",
"DOB" : "12-10-2017",
"Gender" : "Male",
"Dependents" : [
{
"RelationShip" : "Son",
"Name" : "JOHN",
"DOB" : "01-01-1970",
"Gender" : "Male",
"Address" : "Paris",
"ContactNumber" : "+312233445666"
},
{
"RelationShip" : "Wife",
"Name" : "Marry",
"DOB" : "01-01-1980",
"Gender" : "Female",
"Address" : "Paris",
"ContactNumber" : "+312233445666"
}
]
"Matrimony" : [
{
"Fee" : 1000.0,
"FromDate" : "01-01-2015",
"ToDate" : "01-01-2017",
"Status" : false
}
],
"MagazineSubscription" : [
{
"MagazineCode" : "WSS",
"DateFrom" : "01-05-2018",
"DateTo" : "01-01-2020",
"PaidAmount" : 1000.0,
"ActualCost" : 1500.0,
"Status" : false,
"DeliveryStatus" : [
{
"ReturnedDate" : "10-01-2019",
"Comment" : "Returned because of invalid address"
},
{
"ReturnedDate" : "10-02-2019",
"Comment" : "Returned because of invalid address"
}
]
}
]
}
Use mongodb's update operation with upsert:true.
Please refer here: https://docs.mongodb.com/manual/reference/method/db.collection.update/
Here's a sample from the page:
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>, //you need this option
multi: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ]
}
)
And here's a similar question according to what you need:
Upserting in Mongo DB using official C# driver
EDIT 1
Steps:
First you need to write a filter to scan if the document exists. you can check any number of keys (Essentially a document).
Write the update section with the keys you'd like to update (Essentially a document).
Set upsert to true.
Mongodb will use your filter to search the document. If found, it will use the update section to perform the update mentioned by you.
In case the document does not exist, a new document will be created by using the filter keys + keys in the update part.
Hope that makes things clear as I have never used a C# mongo driver. So I won't be able to provide you the exact syntax.
EDIT 2
I'm providing #jeffsaracco's solution here:
MongoCollection collection = db.GetCollection("matches");
var query = new QueryDocument("recordId", recordId); //this is the filter
var update = Update.Set("FirstName", "John").Set("LastName","Doe"); //these are the keys to be updated
matchCollection.Update(query, update, UpdateFlags.Upsert, SafeMode.False);
Given a document structure:
[{
_id: ObjectId("someId"),
Property: {
CollectionProperty: [{
ItemKey: 28,
StringArray: [
"ItemA",
"ItemB"
]
}]
}
}]
Performing the following operation in the mongo shell does what I want:
db.getCollection('collection').updateOne(
{
"_id" : ObjectId("someId"),
"Property.CollectionProperty.ItemKey" : 28
},
{ $push :
{
"Property.CollectionProperty.$.StringArray" : {
$each : [ "AItemA" ],
$sort : 1
}
}
});
However, I cannot find in the documentation or anywhere on the net how to reproduce THAT $sort command in C# code. I can reproduce if it had fields by using "field":1, but not a non-document array like that.
The mongodb documentation that describes that sort is here:
MongoDB Sort Elements that are not documents
Here's what I have in C# so far:
Builders<DataType>.Update.PushEach("Property.CollectionProperty.$.StringArray",
new[] { "AItemA" }, null, null, ???)));
The ??? is what I don't know how to do... does anyone have any suggestions?
TIA
This is apparently currently unsupported, but planned for a future release in the C# driver, as documented here:
https://jira.mongodb.org/browse/CSHARP-1271
I am trying to access MongoDB from C# ASP.NET application.
Let's assume, I've a document like below-
{
"_id" : ObjectId("546c776b3e23f5f2ebdd3b03"),
"Name" : "Test",
"Values" : [
{
"Name" : "One",
"Value" : 1
},
{
"Name" : "Two",
"Value" : 2,
"Parameters": [{"Type": "Type A"}, {"Type": "Type B"}]
}
]
}
Please note that, only the _id and Name elements are fixed; other elements are dynamically created by the user where both the key and value are defined by the user.
Now, I would like to search for the element Type with the value Type A. How can I do this from a MongoDB C# driver?
You can use this code:
var query = Query.EQ("Values.Parameters.Type", "Type A");
var items = collection.Find(query).ToList();
If you data has structure use this:
var items = collection.FindAs<Item>(query).ToList();
Edit:
For dynaically search the only way comes to my mind is full-text-search:
Step1: Define a full text-search on all fields via db.test.ensureIndex({"$**" : "text"});
Step2: Search your query db.test.find( { $text: { $search: "Type A" } } )
If its your answer, the C# code should be easy.
Below aggregation query may solve your problem but I don't know how to write this in C#
db.collectioName.aggregate({"$unwind":"$Values"},
{"$unwind":"$Values.Parameters"},
{"$match":{"Values.Parameters.Type":"Type A"}},
{"$group":{"_id":"$Values"}})
Here is my exact schema:
{
"_id" : ObjectId("4fb4fd04b748611ca8da0d48"),
"Name" : "Categories",
"categories" : [{
"_id" : ObjectId("4fb4fd04b748611ca8da0d46"),
"name" : "Naming_Conventions",
"sub-categories" : [{
"_id" : ObjectId("4fb4fd04b748611ca8da0d47"),
"name" : "Namespace_Naming",
"standards" : []
}]
}]
}
As you can see I have an array named "standards" nested way down in there. How would I programmatically insert in to that using the C# driver? I have tried all of the examples I have found online but none of them are working.
Something like the below. Obviously, if any of these are not present on the way down to it, you're going to get a null reference exception.
var doc = collection.FindOne(Query.EQ("_id", new ObjectId("4fb4fd04b748611ca8da0d48")));
var standards = doc["categories"]
.AsBsonArray[0]
.AsBsonDocument["sub-categories"]
.AsBsonArray;
standards.Add(new BsonDocument());
collection.Save(doc);