Say I have:
db.test.insert({foos: [{bars: [{}, {}]}]});
db.test.insert({foos: [{bars: [{}, {}]}]});
Now I want to remove all bars-fields of all foos. How do I do that?
db.test.update({}, {$unset: {"foos.bars": 1}});
and
db.test.update({}, {$pull: {"foos.bars": {}}});
doesn't do anything.
db.test.update({}, {$pull: {"foos.$.bars": {}}});
gives error:
"Cannot apply the positional operator without a corresponding query field containing an array."
Any help highly appreciated.
Adding an exist-query on the subdocument in the array seemed to do the trick:
db.test.remove();
db.test.insert({foos: [{bars: [{"baz": 1} ]}]});
db.test.insert({foos: [{bars: [{"baz": 1} ]}]});
db.test.find({'foos.bars': {$exists: true}}).count();
db.test.update({'foos.bars': {$exists: true}}, {$unset: {'foos.$.bars': 1}}, false, true);
db.test.find({'foos.bars': {$exists: true}}).count();
Related
I want to query mongoDB field for data that is '$in' two lists (arrays) kinda:
{user: {$in: [<array1>]} AND user: {$in: [<array2>]}}
so the query returns only those 'users' that present in both arrays.
I feel this isn't right:
{user: {$in: [<array1>], [<array2>]}}
Any ideas?
Either you can use the $AND operator.
http://docs.mongodb.org/manual/reference/operator/query/and/
{'$AND':[ {'user':{$in: [<..array1..>]} },{'user':{$in: [<..array2..>] } }] }
If I have a nested array in my schema, how do I tell MongoDB to remove its entries for a specific model?
Schema
var UserSchema = new Schema({
username: String,
documents: [Number]
});
I tried something like this:
db.users.update({"username": "tom"}, {"$pullAll": {"documents": []}})
But the items in the nested array are still there.
Your code is not working, because $pullAll requires list of items which should be removed from array. You are passing empty array, thus nothing is removed.
You can simply set documents to empty array instead of removing all items:
db.users.update({"username": "tom"}, {"$set": {"documents": []}})
If you want to avoid creating documents array if "tom" do not have it, then check if array exists when selecting document to update:
db.users.update({username: "tom", documents: {$exists: true}},
{$set: {documents: []}})
UPDATE: Another option to remove all array items is to use $pull with query which satisfies all documents:
db.users.update({username: "tom"}, {$pull: {documents: {$exists: true}}})
The mongo documentation says the $not operator does what I want, but it doesn't seem to be working:
The following returns a single document:
db.user.find({_id:ObjectId("51f09113cc0bd4a4a3958c96")})
This returns all 27 documents:
db.user.find()
This returns no documents:
db.user.find({$not:{_id:ObjectId("51f09113cc0bd4a4a3958c96")}})
So what am I doing wrong?
You should use $ne:
db.user.find({"_id" : {$ne: ObjectId("51f09113cc0bd4a4a3958c96") }})
Use $ne
db.user.find(_id: {$ne: ObjectId("51f09113cc0bd4a4a3958c96")})
I want to move my data from the column "demographic.education" into "demographic.school". How could i do this?
For example:
db.users.update({"demographic.education":{$exists: true, $ne: ""}}, {$set: {"demographic.school":demographic.education}})
You can use $rename modifier for this:
db.users.update({"demographic.education": {$exists: true, $ne: ""}}, {$rename: {"demographic.education": "demographic.school"}})
Documentation
I want to remove all embedded documents from a collection, but I can't figure out how to do that.
I have tried a few ways, but I think this one should work:
Products.update({_id: data._id}, { $pull : { orders : {$gte: 0} } });
Products is the collection, orders is the array with embedded documents.
Remove all orders where the index is greater or equal than 0.
No luck.
Try this
db.products.update({_id: data._id},
{$unset: {orders: 1}})
or this
db.products.update({_id: data._id},
{$set: {orders: []}})