Delete multiple items in array when position (index) is unknown - mongodb

I have the folowing Mongo db schema :
"Price_History" : [
{
"pr" : 62,
"BookingDateTo" : "2014-08-05T18:41:35",
"CollectionDate" : "2014-07-22T18:41:35",
"Vendor" : "test",
"BookingDateFrom" : "2014-08-04T18:41:35"
},
{
"BookingDateFrom" : "2014-08-04T23:01:37",
"BookingDateTo" : "2014-08-05T23:01:37",
"pr" : 62,
"Vendor" : "test",
"CollectionDate" : "2014-07-19T23:01:37"
}
],
this should be:
"Price_History" : [
{
"pr" : 62,
"BookingDateTo" : "2014-08-05T18:41:35",
"CollectionDate" : "2014-07-22T18:41:35",
"Vendor" : "Trv",
"BookingDateFrom" : "2014-08-04T18:41:35"
},
The query should delete multiple items in an array if Price_History.CollectionDate is older then 2014-07-19T23:01:37
Finally the query should update all documents
is that posible ?

You're close, but you've got some mis-matched quotes in your keys which is breaking things and it should be:
db.coll.update({},
{$pull: {Price_History: {CollectionDate: {$lte : "2014-07-19T23:01:37"}}}},
{multi: true})
I also changed it to use $lte since you were looking for "older than". Using $pull instead of $pullAll is also important here as $pullAll only removes elements that match exactly while $pull performs a query match.

Related

How to PULL multiple value from a list in a document in mongoDB? [duplicate]

Hi I'm trying to remove multiple objects from an array that looks like this.
{
"_id" : ObjectId("5a7da1bda21d5f3e8cf005b3"),
"owner" : "1",
"group_name" : "PAASCU Board",
"group_members" : [
{
"faculty_name" : "Cheska Dela Rosa",
"faculty_number" : 2,
"_id" : ObjectId("5a7da1bda21d5f3e8cf005b5")
},
{
"faculty_name" : "Earl Sempio",
"faculty_number" : 7323,
"_id" : ObjectId("5a7da1bda21d5f3e8cf005b4")
},
{
"faculty_number" : 203,
"faculty_name" : "Sample",
"_id" : ObjectId("5a7dbf7952bd150a94d83958")
},
{
"faculty_number" : 8025,
"faculty_name" : "Sample Postman",
"_id" : ObjectId("5a7dc64a1cf5dd3d50167d53")
}
],
"__v" : 0 }
It works when I remove a single object using the $pull with this code.
db.getCollection('groups').update({_id: ObjectId("5a7da1bda21d5f3e8cf005b3")}, {$pull: {"group_members": {"faculty_number":8025}}})
But what if I want to remove multiple objects with different faculty_number? I tried using the $each method just like how I add multiple objects in the array but it doesn't work well.
Use $in operator to pass the list of faculty values to remove documents from embedded array. More here
Try
db.groups.update(
{"_id": ObjectId("5a7da1bda21d5f3e8cf005b3")},
{"$pull":{"group_members":{"faculty_number":{$in:[8025,7323]}}}}
)

Update multiple data in single array element using object id using mongodb

I am Very Beginner in mongodb.I have mongodb data with multiple array elements just update multiple data in single array element
"_id" : ObjectId("6217138f0607ea2e07e70b25"),
"phone_number" : "7645645676",
"email" : "test02#gmail.com",
"name" : "John",
"address" : "44th street Englan",
"devices" : [
{
"ime_number" : "123456789012345",
"device_name" : "Test456356",
"subscription_type" : "Yearly",
"validity" : 365,
"_id" : ObjectId("6217138f0607ea2e07e70b26"),
},
{
"ime_number" : "HHH555",
"device_name" : "Harry Potter",
"subscription_type" : "Monthly",
"validity" : 32,
"_id" : ObjectId("621714570607ea2e07e70b2b"),
}
]
Only This data
"ime_number" : "123456789012345",
"device_name" : "Test456356",
"subscription_type" : "Yearly",
"validity" : 365,
Changed data
"ime_number" : "888844442222",
"device_name" : "Remote Device",
"subscription_type" : "Monthly",
"validity" : 30,
Help me to solve this
I think you can use $ operator to do this.
The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array.
Now you have two options, to either update the individual fields (only the ones you want to) or update the whole object. For both of them you need $set :
Just pass the query as first argument of your update query and the second argument is your update clause.
Collection.update({'devices.device_name' : 'Test456356'}, {
{ '$set': { 'devices.$': {
"ime_number" : "888844442222",
"device_name" : "Remote Device",
"subscription_type" : "Monthly",
"validity" : 30,
} } }
});
Note: above updates all the elements found with the query. If you want to be specific you can use updateOne. Just replace update with updateOne and it will update the first matched document

How to iterate a mongo db document and update a nested array

I need to remove entries inside a 2d array of documents structure in mongodb.
Sample is as below
{
"_id" : ObjectId("5ffef283f1f06ff8524aa2c2"),
"applicationName" : "TestApp",
"pName" : "",
"environments" : [],
"stages" : [],
"createdAt" : ISODate("2021-01-15T09:51:35.546Z"),
"workflows" : [
[
{
"pName" : "Test1",
"wName" : "TestApp_Test1",
"agent" : ""
},
{
"pName" : "Test2",
"wName" : "TestApp_Test2",
"agent" : ""
}
],
[
{
"pName" : "Test1",
"wName" : "TestApp_Test1",
"agent" : ""
}
]
],
"updatedAt" : Date(-62135596800000)
}
So, I want to remove all document occurences of
{
"pName" : "Test1",
"wName" : "TestApp_Test1",
"agent" : ""
}
from both the arrays.
pName:Test1 can be used to filter it out.
What's the query which can be used to find this and delete it ?
I am ok to do a find and then iterate over the collection , find the matching entry and then update the document. I have to do this using Go mongodb driver
Since this is kind of complex for me as I am new to both golang and mongo db, I am stuck.
Update:
1.Tried {$pull: {workflows: {pName:"Test1"}}}, {multi: true} in the update() call as suggested but didn't work.
2.Tried something like this
db.getCollection('workflows').update({_id:ObjectId('5ffef283f1f06ff8524aa2c2')}, {$pull:{workflows: { $elemMatch: {pName:'Test2'}}}} )
This is removing the entire array as shown below because Test2 is present in that. I need to remove only the Test2 document
[
{
"pName" : "Test1",
"wName" : "TestApp_Test1",
"agent" : ""
},
{
"pName" : "Test2",
"wName" : "TestApp_Test2",
"agent" : ""
}
]
I'll give you a hint as you are new and you haven't posted the tried things.
You could use $pull to remove documents matching the conditions.
similar example
And you could use official mongodb driver for go or most used gopkg
An example SO post

db.collections.updateOne(filter,update,options) not working with $pull operator

I am just running a db.collections.updateOne() query on mongo shell with $pull operator. It works as expected with $set operator but doesn't affect the document with $pull. Am I missing something here?
Thanks
I found an answer to the same problem someone else posted here How to remove embedded scheme document in mongoose?
I dont seem to see where I am going wrong with this
db.vendors.findOne({"_id":ObjectId("5da0343c724f9d0531eeaa4a")})
This is the document I need to update:
{
"_id" : ObjectId("5da0343c724f9d0531eeaa4a"),
"elements" : [
ObjectId("5da03451724f9d0531eeaa4b")
],
"packages" : [ ],
"title" : "test again",
"slug" : "celebration-company",
"__v" : 1,
"department" : ObjectId("5d9892b1c46b6306b2ce64aa")
}
Here is what I am running on the shell:
db.vendors.updateOne({"_id":ObjectId("5da0343c724f9d0531eeaa4a")},
{$pull:
{"elements":
{"_id":ObjectId("5da03451724f9d0531eeaa4b")}
}
})
Result:
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 0 }

mongodb $elemMatch using $ne not working as expected

I am trying to return events where someone has not been invited to. However,
all my queries are returning data. Nothing should be returned when I run the query. What am I missing?
"__v" : 0,
"_id" : ObjectId("565cca79a9baa9b1522b57eb"),
"attendees" : [
{
"_id" : ObjectId("565cca79a9baa9b1522b57ec"),
"attendee" : ObjectId("557dfb4fc8c9ecbb07c2f98c"),
"statustext" : "Accepted",
"status" : 1
},
{
"attendee" : ObjectId("55dec11f38180102145d0060"),
"_id" : ObjectId("565f6bacdcbac0a6a354420c"),
"statustext" : "Pending",
"status" : 0
}
]
db.events.find({attendees:{$elemMatch:{attendee:{$ne:"55dec11f38180102145d0060"}}}}).
db.events.find({attendees:{$elemMatch:{attendee:{$ne:'55dec11f38180102145d0060'}}}})
db.events.find({attendees:{$elemMatch:{attendee:{$ne:ObjectId('55dec11f38180102145d0060')}}}})
Quoting the docs:
The $elemMatch operator matches documents that contain an array field
with at least one element that matches all the specified query
criteria.
That means the $elemMatch is not suited for this case.
db.events.find({"attendees.attendee":{$ne: ObjectId("55dec11f38180102145d0060")}})