How to delete multiple ids in mongodb? - mongodb

{ "_id" : ObjectId("51ee3966e4b056fe8f074f48"), "userid" : "66", "clientid" : "88", "deviceid" : "22", "timestamp" : "1374214822000"}
{ "_id" : ObjectId("51ee507ae4b056fe8f074f4a"), "userid" : "66", "clientid" : "88", "deviceid" : "22", "timestamp" : "1374214822000"}
{ "_id" : ObjectId("51ee51fee4b056fe8f074f4b"), "userid" : "66", "clientid" : "88", "deviceid" : "22", "timestamp" : "1374214822000"}
How to delete multiple ids in mongodb?

By running remove once for each ID, or perhaps using in:
db.collection.remove( { _id : { $in: [
ObjectId("51ee3966e4b056fe8f074f48"),
ObjectId("51ee3966e4b056fe8f074f4a"),
ObjectId("51ee3966e4b056fe8f074f4b")
] } } );

You can accomplish this by using the command deleteMany.
const objects = [
ObjectId("51ee3966e4b056fe8f074f48"),
ObjectId("51ee3966e4b056fe8f074f4a"),
ObjectId("51ee3966e4b056fe8f074f4b")
];
db.collection.deleteMany({_id: { $in: objects}});

Related

How to find and update an array in the object of array document using mongoose

A query to how to update the items array of object by
finding the breed dachshund
update the name to new-dog
updatedAt to the current time
[{
"_id" : ObjectId("60053b74aa72f132cb75b8b6"),
"clientId" : "test",
"items" : [
{
"_id" : ObjectId("60053b74aa72f132cb75b8b7"),
"name" : "tommy",
"breed" : "dachshund",
"createdAt" : 1610955636,
"updatedAt" : 1610955636
},
{
"_id" : ObjectId("60053b74aa72f132cb75b8b8"),
"name" : "mickey",
"breed" : "husky",
"createdAt" : 1610955636,
"updatedAt" : 1610955636
},
{
"_id" : ObjectId("60053b74aa72f132cb75b8b9"),
"name" : "whiskey",
"breed" : "dachshund",
"createdAt" : 1610955636,
"updatedAt" : 1610955636
},
{
"_id" : ObjectId("60053b74aa72f132cb75b8ba"),
"name" : "milo",
"breed" : "bulldog",
"createdAt" : 1610955636,
"updatedAt" : 1610955636
},
{
"_id" : ObjectId("60053b74aa72f132cb75b8bb"),
"name" : "pooh",
"breed" : "poodle",
"createdAt" : 1610955636,
"updatedAt" : 1610955636
}
],
"shopname" : "myshopify"
}]
const curDatetime = moment(moment();
model.updateOne(
{_id: ObjectId("60053b74aa72f132cb75b8b6")},
{
arrayFilters: [
{'el.breed': 'dachshund'}
]
},
{
$set: {
'items.$[el].name': 'new-dog',
'items.$[el].updatedAt': curDatetime
}
}, "multi": true)

Lookup stage not working after unwind operation in aggregate pipeline in mongodb

Being relatively new in mongodb I performed an aggregation pipeline with this three table and surprisingly I am getting the "r2" array as empty in the third lookup stage of the pipeline.I crosschecked the field names and everything is alright.
db.user.aggregate([
{
$lookup:{
from:"enrollment",
localField:"user_id",
foreignField:"userID",
as:"r1"
}
},
{
$unwind:{
path:"$r1",
includeArrayIndex:"r1_id"
}
},
{
$lookup:{
from:"course",
localField:"r1.courseID",
foreignField:"courseID",
as:"r2"
}
}
])
I have three collection as user , enrollment and course which are.
User as
> db.user.find()
{ "_id" : ObjectId("5ef4ba8d500ac8876da0d2ca"), "user_id" : 1, "first_name" : "Christian",
"last_name" : "Hur", "email" : "christian#uta.com", "password" : "abc1234" }
{ "_id" : ObjectId("5ef4ba8d500ac8876da0d2cb"), "user_id" : 2, "first_name" : "Mary", "last_name" :
"Jane", "email" : "mary.jane#uta.com", "password" : "password123" }
{ "_id" : ObjectId("5ef4bc2563742adee5403b1d"), "user_id" : 3, "first_name" : "ari", "last_name" :
"dutta", "email" : "dutta#uta.com", "password" : "po1234" }
And , course as
> db.course.find()
{ "_id" : ObjectId("5ef4c1b64a77aec0af5e73ae"), "courseID" : 3333, "title" : "Adv PHP 201",
"description" : "Advance PHP programming", "credits" : 3, "term" : "fall" }
{ "_id" : ObjectId("5ef4c20d4a77aec0af5e73af"), "courseID" : 5555, "title" : "Java 201",
"description" : "Advanced Programming", "credits" : 4, "term" : "fall"}
{ "_id" : ObjectId("5ef4c2564a77aec0af5e73b0"), "courseID" : 6666, "title" : "Angular 1",
"description" : "Intro to Angular", "credits" : 3, "term" : "fall,spring" }
And enrollment as
> db.enrollment.find()
{ "_id" : ObjectId("5ef771f42d98ffab4460a651"), "userID" : 1, "courseID" : "3333" }
{ "_id" : ObjectId("5ef7722d2d98ffab4460a652"), "userID" : 1, "courseID" : "6666" }
Result
{ "_id" : ObjectId("5ef4ba8d500ac8876da0d2ca"), "user_id" : 1, "first_name" :
"Christian", "last_name" : "Hur", "email" : "christian#uta.com", "password" :
"abc1234", "r1" : { "_id" : ObjectId("5ef771f42d98ffab4460a651"), "userID" :
1, "courseID" : "3333" }, "r1_id" : NumberLong(0), "r2" : [ ] }
{ "_id" : ObjectId("5ef4ba8d500ac8876da0d2ca"), "user_id" : 1, "first_name" :
"Christian", "last_name" : "Hur", "email" : "christian#uta.com", "password" :
"abc1234", "r1" : { "_id" : ObjectId("5ef7722d2d98ffab4460a652"), "userID" :
1, "courseID" : "6666" }, "r1_id" : NumberLong(1), "r2" : [ ] }
I also checked the documentation but found no help , how can I fix this ?
You have a type mismach on courseID between the collections, in enrollment it's type string and in course it's type number.
Change your $lookup into this:
{
$lookup: {
from: "course",
let: {
courseID: {
$toInt: "$r1.courseID"
}
},
pipeline: [
{
$match: {
$expr: {
$eq: [
"$$courseID",
"$courseID"
]
}
}
}
],
as: "r2"
}
}
Try it yourself:
Mongo Playground
Also since you say you're new I personally advice you to use the _id field instead of the courseID/user_id that you generated yourself. it will just make it easier to maintain.
For Mongo version 4.2+ here is how to update the enrollment collection field:
db.enrollment.updateMany(
{},
[
{
$set: {
courseID: {$toInt: "$courseID"}
}
}
]
)

Nested object update query in mongodb

I have object in my procedures collection.
I want to update item_status of item_id 5996c80fca423ce1228f7690 which is available in preferences.items.
I want to get all records who has mentioned item_id so that we can update all occurrences.
{
"_id" : ObjectId("5996d0a1ca423ce1228f777a"),
"status" : "Active",
"procedure_name" : "ATHA",
"procedure_for" : "Admin",
"created_by" : "5940c3e8734d1d79866338cf",
"created_on" : ISODate("2017-10-19T18:44:22.702+0000"),
"speciality_id" : "5751131e3a1253845560a984",
"speciality" : "Orthopdics",
"master_template_id" : "",
"hospital_id" : "",
"surgeon_nurse_id" : "",
"procedure_type" : "Published",
"published_on" : ISODate("2017-10-19T18:44:22.702+0000"),
"surgical_sequence" : [
],
"preferences" : [
{
"category_id" : "5971fae84658f5241d8a5b70",
"category" : "Instruments",
"_id" : ObjectId("59e8f2861c999f292a837304"),
"items" : [
{
"item_id" : "5996c80fca423ce1228f7690",
"item_name" : "Battery",
"item_description" : "",
"image_name" : "BATTERY.png",
"icon_image" : "",
"side_view_image" : "",
"_id" : ObjectId("59e8f2861c999f292a837306"),
"attributes" : [
],
"subcategory" : [
{
"name" : "Power tool",
"subcategory_id" : "5996c80eca423ce1228f7549",
"parent_id" : "",
"_id" : ObjectId("5996c80fca423ce1228f7709")
},
{
"name" : "Battery",
"subcategory_id" : "5996c80eca423ce1228f750e",
"parent_id" : "5996c80eca423ce1228f7549",
"_id" : ObjectId("5996c80fca423ce1228f7708")
}
]
}
]
}
],
"__v" : NumberInt(0),
"modified_by" : "5940c3e8734d1d79866338cf",
"modified_on" : ISODate("2017-10-19T18:44:22.702+0000")
}
I'm assuming you are talking about updating status field since I don't find any field with the name item_status. If that's the case, the below query should work:
> db.procedures.updateMany({"preferences.$.items.$.item_id": "5996c80fca423ce1228f7690"}, {"$set": {"status": "new_status"}})

Fetch Unique Document in Mongodb?

Sample Data:
{"_id" : ObjectId("58bd10e4ff1743c527754160"),
"data" : [
{
"No" : "70",
"Type" : "82",
"Device" : "01",
"timestamp" : "2017-03-06 13:00:32"
}]
},
{"_id" : ObjectId("58bd10sdfe4ff1743csdf0754"),
"data" : [
{
"No" : "75",
"Type" : "22",
"Device" : "02",
"timestamp" : "2017-03-06 13:00:32"
}]
}
I have some document which having same timestamp,so I want to find only unique document on the basis of timestamp.
I have done distinct of timestamp but I want full document.
desired Output:
If same timestamp is there I want only One document.
You will only get one output if you run this query.
db.getCollection('tests').aggregate([{$match:{"data.timestamp":"2017-03-06 13:00:32"}},{$limit:1}])
Solution 1:
db.your_collection.aggregate([
{
$group:{
_id:"$data.timestamp",
data:{
$first:"$data"
}
}
}
])
This will give you following :
{ "_id" : [ "2017-03-06 13:00:32" ], "data" : [ { "No" : "70", "Type" : "82", "Device" : "01", "timestamp" : "2017-03-06 13:00:32" }, { "No" : "10", "Type" : "20", "Device" : "01", "timestamp" : "2018-02-04 10:00:00" } ] }
Solution 2 :
db.your_collection.aggregate([
{ $unwind : '$data'},
{ $group : {
_id : '$data.timestamp',
'No': { $first : '$data.No'},
'Type': { $first : '$data.Type'},
'Device': { $first : '$data.Device'},
'timestamp': { $first : '$data.timestamp'},
}
}
]);
This will give you following :
[
{ "_id" : "2017-03-06 13:00:32", "No" : "70", "Type" : "82", "Device" : "01", "timestamp" : "2017-03-06 13:00:32" },
{ "_id" : "2018-02-04 10:00:00", "No" : "10", "Type" : "20", "Device" : "01", "timestamp" : "2018-02-04 10:00:00" },
]

Mongo DB : remove document from collection

Here is one of my document of my collection "entities", I can't figure out how to remove it.
{
"_id" : {
"id" : "sensors:StreetLight2",
"type" : "sensors",
"servicePath" : "/egmmqttpath"
},
"attrNames" : [
"TimeInstant",
"PING_status"
],
"attrs" : {
"PING_status" : {
"value" : "delivered but no respond",
"type" : "string",
"md" : [
{
"name" : "TimeInstant",
"type" : "ISO8601",
"value" : "2015-11-20T09:02:53.114688"
}
],
"creDate" : 1448010161,
"modDate" : 1448010172
},
"TimeInstant" : {
"value" : "2015-11-20T09:02:53.114834",
"type" : "ISO8601",
"creDate" : 1448010122,
"modDate" : 1448010172
}
},
"creDate" : 1448010122,
"modDate" : 1448010172
}
Any ideas? how can I remove the above document? Thanks in advance.
From doc:
db.collection.remove()
Removes documents from a collection.
1) By _ID: Since _ID is unique.
db.entities.remove( {"_id" :
{
"id" : "sensors:StreetLight2",
"type" : "sensors",
"servicePath" : "/egmmqttpath"
}
})
I would pass the _id to remove it.
db.entities.remove({
"_id" : {
"id" : "sensors:StreetLight2",
"type" : "sensors",
"servicePath" : "/egmmqttpath"
}
})
remove document in mongodb
query = {"_id": <value of _id>}
db.entities.remove(query)
db.entities.remove(
"_id" : {
"id" : "sensors:StreetLight2",
"type" : "sensors",
"servicePath" : "/egmmqttpath""id" : "sensors:StreetLight2"
}
)
you can also take any key-value pair from documents for remove
you can refer these links for better understanding
https://docs.mongodb.org/manual/tutorial/remove-documents/
https://docs.mongodb.org/manual/reference/method/db.collection.remove/
In mongo shell you should use .remove():
db.entities.remove(
{
"_id" :
{
"id" : "sensors:StreetLight2",
"type" : "sensors",
"servicePath" : "/egmmqttpath"
}
})