How to remove a particular element from a nested array in mongodb - mongodb

My Document structure is
"MainAccounts" : [
{
"orgs" : "5808ba773fe315441b9e0a9e",
"_id" : ObjectId("5808bc0c3fe315441b9e0b1a"),
"accounts" : [
"5808baf33fe315441b9e0aa7",
"5808baf33fe315441b9e0aa8",
"5808baf33fe315441b9e0aa9",
"5808baf33fe315441b9e0aa1"
]
},
{
"orgs" : "5808ba773fe315441b9e0a9f",
"_id" : ObjectId("5808bc0c3fe315441b9e0b1b"),
"accounts" : [
"5808baf33f35425852s255s7",
"5808baf3sd23s2d3d4w5s2s8",
"5808baf33sd211ds2d2sdsa9",
"5808baf33dssd2d21b9e0aa1"
]
}
],
I want to pull out a particular account say "5808baf33fe315441b9e0aa8" from this, i wrote the query like this.
{ $pull: { "MainAccounts.$.accounts": "5808baf33fe315441b9e0aa8"} }
It gives only error as "The positional operator did not find the match needed from the query. Unexpanded update: MainAccounts.$.accounts"
{ $pull: { "MainAccounts.0.accounts": "5808baf33fe315441b9e0aa8" } }
If i give like this it will remove that value only which gives the expected output.
i need output as
"MainAccounts" : [
{
"orgs" : "5808ba773fe315441b9e0a9e",
"_id" : ObjectId("5808bc0c3fe315441b9e0b1a"),
"accounts" : [
"5808baf33fe315441b9e0aa7",
"5808baf33fe315441b9e0aa9",
"5808baf33fe315441b9e0aa1"
]
},
{
"orgs" : "5808ba773fe315441b9e0a9f",
"_id" : ObjectId("5808bc0c3fe315441b9e0b1b"),
"accounts" : [
"5808baf33f35425852s255s7",
"5808baf3sd23s2d3d4w5s2s8",
"5808baf33sd211ds2d2sdsa9",
"5808baf33dssd2d21b9e0aa1"
]
}
],
here i am not able to delete value from second array i need to give
{ $pull: { "MainAccounts.1.accounts": "5808baf33fe315441b9e0aa8" } }
But i need to loop through, any help is appreciated.

You will get an error:
"Cannot apply $pull to a non-array value"
This should be :
db.collection.update({'MainAccounts.accounts': '5808baf33fe315441b9e0aa8'}, {$pull: {MainAccounts:{ accounts: '5808baf33fe315441b9e0aa8'}}})
Here is a reference to this:
mongodb Cannot apply $pull/$pullAll modifier to non-array, How to remove array element

db.collection.update({someId,{$pull : {"MainAccounts":{"accounts":"5808baf33fe315441b9e0aa8"}}}})
someId could be your _id.
Remember if you have to access the document inside the array you cant access it without . operator only.You have to use the index with it.The other way mongodb can access it is by the use of braces.

This will do what you want:
db.collection.update({'MainAccounts.accounts': '5808baf33fe315441b9e0aa8'}, {$pull: {'MainAccounts.$.accounts': '5808baf33fe315441b9e0aa8'}})

Related

How to remove an element from inner array of nested array pymongo using $ pull

Here is my news document structure
{
"_id" : ObjectId("5bff0903bd9a221229c7c9b2"),
"title" : "Test Page",
"desc" : "efdfr",
"mediaset_list" : [
{
"_id" : ObjectId("5bfeff94bd9a221229c7c9ae"),
"medias" : [
{
"_id" : ObjectId("5bfeff83bd9a221229c7c9ac"),
"file_type" : "png",
"file" : "https://aws.com/gg.jpg",
"file_name" : "edf.jpg"
},
{
"_id" : ObjectId("5bfeff83bd9a221229c7c9ad"),
"file_type" : "mov",
"file" : "https://aws.com/gg.mov",
"file_name" : "abcd.mov"
}
]
}
]}
The queries that i've tried are given below
Approach 1
db.news.find_and_modify({},{'$pull': {"mediaset_list": {"medias": {"$elemMatch" : {"_id": ObjectId('5bfeff83bd9a221229c7c9ac')}} }}})
Approach 2
db.news.update({},{'$pull': {"mediaset_list.$.medias": {"_id": ObjectId('5bfeff83bd9a221229c7c9ac')}} })
Issue we are facing
The above queries are removing entire elements inside 'mediaset_list' . But i only want to remove the element inside 'medias' matching object ID.
Since you have two nested arrays you have to use arrayFilters to indicate which element of outer array should be modified, try:
db.news.update({ _id: ObjectId("5bff0903bd9a221229c7c9b2") },
{ $pull: { "mediaset_list.$[item].medias": { _id: ObjectId("5bfeff83bd9a221229c7c9ad") } } },
{ arrayFilters: [ { "item._id": ObjectId("5bfeff94bd9a221229c7c9ae") } ] })
So item is used here as a placeholder which will be used by MongoDB to determine which element of mediaset_list needs to be modified and the condition for this placeholder is defined inside arrayFilters. Then you can use $pull and specify another condition for inner array to determine which element should be removed.
From #micki's mongo shell query (Answer above) , This is the pymongo syntax which will update all news document with that media id .
db.news.update_many({},
{
"$pull":
{ "mediaset_list.$[item].medias": { "_id": ObjectId("5bfeff83bd9a221229c7c9ad") } } ,
},
array_filters=[{ "item._id": ObjectId("5bfeff94bd9a221229c7c9ae")}],
upsert=True)

Filter sub document array

db.doc.find({},{"sections.rows.Desk":1}) returns desks list but also empty rows i.e. where desk attribute doesn't exist in rows array...
I would like to eliminate empty results. How to go about it?
Thanks!
My document "doc" has the following format.
"docs":
{
"sections" : [
{
"name" : "Request Details",
"rows" : [
{
"Desk" : "IT4"
}
]
},
{
"name" : "Approval",
"rows" : [
{
"Approval" : ""
}
]
}
]
}
You will have to use the aggregation framework, particularly the $unwind operator to be able to query against individual array elements and get the ones you are after. Currently with the way .find works you will get the entire document if it matches your query.
db.docs.aggregate({$unwind: "$sections"}, {$match: {"sections.rows.Desk": {$exists: 1}}});

check if value exists in array field in mongodb

I want to check if user id exists inside an array field of mongodb (using meteor)
db.posts.find().pretty()
{
"_id" : "hT3ezqEyTaiihoh6Z",
"body" : "hey\n",
"authorId" : "AyJo5nf2Lkdqd6aRh",
"createdAt" : ISODate("2016-05-13T06:19:34.726Z"),
"updatedAt" : ISODate("2016-05-13T06:19:34.726Z"),
"likecount" : 0,
"already_voted" : [ ] }
db.posts.find( { _id:"hT3ezqEyTaiihoh6Z"},{ already_voted: { $in : ["AyJo5nf2Lkdqd6aRh"]} }).count()
1
It gives count value 1 , where as I am expecting it to be 0 .
Your logic is fine. Just the syntax is wrong.
db.posts
.find({
_id: "hT3ezqEyTaiihoh6Z",
already_voted: { $in: ["AyJo5nf2Lkdqd6aRh"] },
})
.count();
This should work.
You can just simply use count method. Don't need to use two operation like Find and then count.
db.posts
.count({
_id: "hT3ezqEyTaiihoh6Z",
already_voted: { $in: ["AyJo5nf2Lkdqd6aRh"] }
});

In a Mongo collection, how do you query for a specific object in an array?

I'm trying to retrieve an object from an array in mongodb. Below is my document:
{
"_id" : ObjectId("53e9b43968425b29ecc87ffd"),
"firstname" : "john",
"lastname" : "smith",
"trips" : [
{
"submitted" : 1407824585356,
"tripCategory" : "staff",
"tripID" : "1"
},
{
"tripID" : "2",
"tripCategory" : "volunteer"
},
{
"tripID" : "3",
"tripCategory" : "individual"
}
]
}
My ultimate goal is to update only when trips.submitted is absent so I thought I could query and determine what the mongo find behavior would look like
if I used the $and query operator. So I try this:
db.users.find({
$and: [
{ "trips.tripID": "1" },
{ "trips": { $elemMatch: { submitted: { $exists: true } } } }
]
},
{ "trips.$" : 1 } //projection limits to the FIRST matching element
)
and I get this back:
{
"_id" : ObjectId("53e9b43968425b29ecc87ffd"),
"trips" : [
{
"submitted" : 1407824585356,
"tripCategory" : "staff",
"tripID" : "1"
}
]
}
Great. This is what I want. However, when I run this query:
db.users.find({
$and: [
{ "trips.tripID": "2" },
{ "trips": { $elemMatch: { submitted: { $exists: true } } } }
]
},
{ "trips.$" : 1 } //projection limits to the FIRST matching element
)
I get the same result as the first! So I know there's something odd about my query that isn't correct. But I dont know what. The only thing I've changed between the queries is "trips.tripID" : "2", which in my head, should have prompted mongo to return no results. What is wrong with my query?
If you know the array is in a specific order you can refer to a specific index in the array like this:-
db.trips.find({"trips.0.submitted" : {$exists:true}})
Or you could simply element match on both values:
db.trips.find({"trips" : {$elemMatch : {"tripID" : "1",
"submitted" : {$exists:true}
}}})
Your query, by contrast, is looking for a document where both are true, not an element within the trips field that holds for both.
The output for your query is correct. Your query asks mongo to return a document which has the given tripId and the field submitted within its trips array. The document you have provided in your question satisfies both conditions for both tripIds. You are getting the first element in the array trips because of your projection.
I have assumed you will be filtering records by the person's name and then retrieving the elements inside trips based on the field-exists criteria. The output you are expecting can be obtained using the following:
db.users.aggregate(
[
{$match:
{
"firstname" : "john",
"lastname" : "smith"
}
},
{$unwind: "$trips"},
{$match:
{
"trips.tripID": "1" ,
"trips.submitted": { $exists: true }
}
}
]
)
The aggregation pipeline works as follows. The first $match operator filters one document (in this case the document for john smith) The $unwind operator in mongodb aggregation unwinds the specified array (trips in this case), in effect denormalizing the sub-records associated with the parent records. The second $match operator filters the denormalized/unwound documents further to obtain the one required as per your query.

Removing a (sub) array in MongoDB using $pull

So..I'm evaluating Mongodb for managing a bit of my JSON back end. I'm totally new to it and I had one problem that was just messy to do in code, so I thought — heck — let me check out to see if it's time to finally start using Mongo
I have a data structure that is approximately like this:
[
{
"_id" : ObjectId("526f59ee82f2e293f9833c54"),
"humans" : [
{
"serviceUsers" : [
{
"foo1" : "bar2",
"foo2" : "bar3"
},
{
"foo1" : "baz2",
"foo2" : "baz3"
}
]
}
]
}
]
And now I want to remove any serviceUsers array elements that have "foo1" equal to "baz2" so that ideally I would end up with this:
[
{
"_id" : ObjectId("526f59ee82f2e293f9833c54"),
"humans" : [
{
"serviceUsers" : [
{
"foo1" : "bar2",
"foo2" : "bar3"
},
]
}
]
}
]
I figured that $pull was the place to start. And I tried a bunch of contortions. If I'm in collection mytests, I tried
db.mytests.update({"humans.serviceUsers.foo1":"baz2"}, {$pull:{"humans.serviceUsers" : {"foo1":"baz2"}}}, {multi: true})
Which to my admittedly naive eye, seems like it should follow the $pull syntax:
db.collection.update( { field: <query> }, { $pull: { field: <query> } } );
Mongo doesn't complain. But it doesn't change the collection in any way, either.
I also tried
db.mytests.update({}, {$pull:{"humans.serviceUsers" : {"foo1":"baz2"}}}, {multi: true})
Which also failed.
Any suggestions are greatly appreciated.
Thus humans is also array, you should use positional $ operator to access serviceUsers array of matched humans element:
db.mytests.update({ "humans.serviceUsers.foo1" : "baz2" },
{ $pull: { "humans.$.serviceUsers" : { "foo1": "baz2" }}});