I have searched for the usage of conditional statements(<,>,<=,etc..,) in C API documentation in the link http://api.mongodb.org/c/current/. But I am unable to find it.
Example:
The mongo shell query is
db.users.find({age: {$gt: 3}}, {})
I want the equivalent C statement for the above.
For example, query:
find({ age : { $gt : 5, $lt : 12}})
would be written like this:
bson_init(&b);
bson_append_start_object(&b,"age");
bson_append_int(&b,"$gt",5);
bson_append_int(&b,"$lt",12);
bson_append_finish_object(&b);
bson_finish(&b);
Related
I am trying to do what the title of the question says. Removing a single item from an array of objects. I am trying to remove a specific object using the _id (ObjectID). I have written this query as it can be seen on the code below : $pull: { 'ARRAY': {$elemMatch: {_id: idToRemove}}}. What happens next may shock you. ALL items from the array are removed...
Here is the code from the nodeJS app that sends the query:
var findOne = { 'User.username': req.user.User.username };
var query = { $pull: { 'User.Polls': {$elemMatch: {_id: req.body.pollID}}}};
User.findOneAndUpdate(findOne, query, function (err, response) {//EVERYTHING GOT REMOVED});
Here is a photo of the mongod structure
I solved it myself. I was using User.Polls._id which did not point into the array that i had(Polls). After that I got it to work by selecting the whole document using $elemMatch creating this bug. By removing $elemMatch my problem got solved. That's the final query $pull: { 'User.Polls': {_id: req.body.pollID}}
Let's say we have the following document:
db.test.insert({a: [2, 3, 4, 5, 6, 7, 8]})
How to remove all even numbers and just left [3, 5, 7]? Can this be accomplished by using the $pull operator?
You can pull all even elements from your array using the $pull update operator and the $mod query operator. To update multiple documents I suggest you use the updateMany method and for single update you should use the updateOne method because update is highlighted as deprecated in official language driver. The problem is that shell method lagged behind the drivers. Also nobody really writes application in the shell.
db.test.updateMany({}, { "$pull": { "a": { "$mod": [ 2, 0 ] } } } )
Considering:
{
docs: Array[{field1, field2}]
}
I know how to find the document(s) containing field1 or field2 with $elemMatch, but my question is how can I get the value of field1 knowing field2?
I also know that in MongoDB you would use $elemMatch in the projection parameter. Or that I can do this in JS with something like _.find(), but the goal is to get only one specific document from Mongo.
Let say for example a document:
{
_id: 1
docs: [{a: 42, b:"stringX"}, {a:0, b:"stringY"}]
}
How can I get the value of a (42) knowing b ("stringX")?
Is there something like: MyCollection.findOne({_id: 1}, projection: {docs:{b: "stringX"}}) ?
On the server you can use this projection:
var a = MyCollection.findOne({'docs.b': "stringX"},{fields: {'docs.$': 1}}).a;
However the $ array projection does not work on the client in minimongo (yet).
Hopefully this is sufficient to get you unstuck.
See related question
I have a dynamic complex JSON, something like this
var source = [{
"ab" : 123,
"xfg" : {
"cdf" : "xyz",
"e" : [{"aaa" : "bbb"}, {"ccc" : "ccc"}]
},
"mno" : ["fff", "123"]
}];
How can I extract data from this JSON using some dynamic expressions in a given search object:
var search= {
"search1" : "ab",
"search2" : "xfg.cdf",
"search3" : "ccc value in xfg.e?",
}
Basically, I can analyze the type of each element in the search object, if it's a string split it by '.' separator and then access the elements in the source object...
But what about complex search expressions? How do I get the 'ccc' value for example?. Is there a way to implement complex search expressions? something like in mongodb find function?
Thanks
I haven't used this, but the demo looks really nice. It's basically a css type selector for JSON: JSONSelect.
For a more XPath style, try JSONPath, which also looks very capable. JSONPath
Both are Javascript libraries and are easily included in your project.
If I have a field in a mongodb document which has an array, for example:
"tags" : [ "tag", "etc1", "etc2", "etc3" ]
Is there a way that I can select that document if it contains the element 'etc1'?
If I try using the query:
db.coll.find({"tags" : { $elemMatch: { value0: 'etc1'} }})
but I need to know the position of the element in the array, which I don't know.
I have also tried:
db.coll.find({"tags" : { $elemMatch: 'etc1' }})
but it needs to be an object. Is there any way of doing this?
NB I am using mongoose but I wasn't sure how to construct the query
Use the $in operator as described in this question or this other question and as described in the mongodb docs and can be accessed in mongoose.
With it, you'll have a query that looks something like this...remembering that you must pass an array when using $in:
db.coll.find({"tags" : { $in : ['etc1'] } } );
Apart from using $in operator you can also use $all operator like:
db.col1.find({"tags":{$all :["etc1"]}})