MongoDB DeleteMany not deleting documents - database

So, when i run this query to delete some friend request documents:
db.friendRequests.deleteMany([
{
$and: [
{ author: ObjectId("636a88de3e45346191cf4257") },
{ friend_id: ObjectId("636faabb1861c5b8773f751e") },
],
},
]);
then it doesn't delete anything, and i dont know why.
Heres the friendRequests document exported as JSON from MongoDB Compass:
[{
"_id": {
"$oid": "63764d512bf0d70cbce32664"
},
"author": {
"$oid": "636fa1ce289c56d125ac8a4a"
},
"friend_id": {
"$oid": "636a88de3e45346191cf4257"
},
"request_at": {
"$date": {
"$numberLong": "1668697425541"
}
},
"__v": 0
},{
"_id": {
"$oid": "63764d622bf0d70cbce32673"
},
"author": {
"$oid": "636cf37d8a8b51a38932d3d8"
},
"friend_id": {
"$oid": "636a88de3e45346191cf4257"
},
"request_at": {
"$date": {
"$numberLong": "1668697442809"
}
},
"__v": 0
},{
"_id": {
"$oid": "637cc8e760709f87c46ad5a9"
},
"author": {
"$oid": "636a88de3e45346191cf4257"
},
"friend_id": {
"$oid": "636faabb1861c5b8773f751e"
},
"request_at": {
"$date": {
"$numberLong": "1669122279674"
}
},
"__v": 0
}]
Can you help me why it doesn't delete anything? Thanks!

I figured it out.
I just had to put a .exec() after the deleteMany query

Related

push object into array field in document in mongoDb

Schema
{
userid:{type:String,required:true,unique:false},
products:[{
productid:{
type:String
},
quantity:{
type:Number,
default:1,
},
}],
}
existing document:
{
"_id": {
"$oid": "633a8a8a3cb2f7a3e9703261"
},
"userid": "633a8a833cb2f7a3e970325c",
"products": [
{
"productid": "633993d09725095d71b071db",
"quantity": 1,
"_id": {
"$oid": "633a8a8a3cb2f7a3e9703262"
}
}
],
"createdAt": {
"$date": {
"$numberLong": "1664780938934"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1664780938934"
}
},
"__v": 0
}
now i want to push below object to products array
"products":{productid:"6786879",quantity,2}
i tried below command (carts is collection name)
db.carts.update( { userid : "633a8a833cb2f7a3e970325c" },{ $push: { "products":
{productid:"6786879",quantity,2} } });
it throws below error :
clone(t={}){const r=t.loc||{};return e({loc:new Position("line"in r?
r.line:this.loc.line,"column"in r?r.column:......)} could not be cloned.
please anyone could help me

Finding ID of mongo documents with duplicated elements in nested array

I would like to extract from the collection the IDs of documents that have duplicate IDs of "drives" objects that are nested in the array that is in "streetModel".
This is my typical document :
{
"_id": {
"$oid": "61375bec4fa522001b608568"
},
"name": "Streetz",
"statusDetail": {},
"streetModel": {
"_id": "3.7389-51.0566",
"name": "Kosheen - Darude - Swedish - Trynidad - Maui",
"countryCode": "DEN",
"drives": [{
"_id": -903500698,
"direction": "WEST"
}, {
"_id": 1915399546,
"direction": "EAST"
}, {
"_id": 1294835467,
"direction": "NORTH"
}, {
"_id": 1248969937,
"direction": "EAST"
}, {
"_id": 1248969937,
"direction": "EAST"
}, {
"_id": 1492411786,
"direction": "SOUTH"
}]
},
"createdAt": {
"$date": "2021-09-07T12:32:44.238Z"
}
}
In this particular document with the ID 61375bec4fa522001b608568, in "streetModel", in "drives" array I have got duplicated drives objects with id 1248969937.
I would like to create a query to the database that will return the ID of all documents with such a problem (duplicate "drives").
Right now I have got this:
db.streets.aggregate([
{
$unwind: "$streetModel"
},
{
$unwind: "$drives"
},
{
$group: {
_id: {
id: "$_id"
},
sum: {
$sum: 1
},
}
},
{
$match: {
sum: {
$gt: 1
}
}
},
{
$project: {
_id: "$_id._id",
duplicates: {
drives: "$_id"
}
}
}
])
but that's not it.
I try in many ways to rewrite this query, but unfortunately it doesn't work.
Query
unwind
group by document id + driverid
keep only those that had more than one time same driveid
replace-root is to make the document better looking, you could $project also instead
if you need any more stage i think you can add it, for examplpe to get the documents that have this problem project only the docid's
Test code here
db.collection.aggregate([
{
"$unwind": {
"path": "$streetModel.drives"
}
},
{
"$group": {
"_id": {
"docid": "$_id",
"driveid": "$streetModel.drives._id"
},
"duplicates": {
"$push": "$streetModel.drives.direction"
}
}
},
{
"$match": {
"$expr": {
"$gt": [
{
"$size": "$duplicates"
},
1
]
}
}
},
{
"$replaceRoot": {
"newRoot": {
"$mergeObjects": [
"$_id",
"$$ROOT"
]
}
}
},
{
"$project": {
"_id": 0
}
}
])

How to reverse $unwind or re-assemble after $lookup?

I´ve been trying to reverse $unwind in nested array. Please, if you could help me it would be great. Thanks in advance.
Here are the details:
checklists collection, this collection has steps and each step has many areas, and I'd like to lookup to fill the area by id. I did it but I cannot reverse $unwind.
{
"steps": [{
"name": "paso1",
"description": "paso1",
"estimated_time": 50,
"active": true,
"areas": [{
"area_id": "60b6e728c44f0365c0d547d6"
}, {
"area_id": "60b6e7a2c44f0365c0d547d8"
}]
}, {
"name": "paso2",
"description": "o",
"estimated_time": 7,
"active": true,
"areas": [{
"area_id": "60b6e76ac44f0365c0d547d7"
}]
}, {
"name": "paso2",
"description": "l",
"estimated_time": 7,
"active": true,
"areas": [{
"area_id": "60b6e728c44f0365c0d547d6"
}]
}],
"name": "prueba",
"description": "prueba",
"type": "prueba",
"active": true,
"updated_at": {
"$date": "2021-06-02T23:56:02.232Z"
},
"created_at": {
"$date": "2021-06-01T22:44:57.114Z"
},
"__v": 0
}
area collection
{
"_id":"60b6e706c44f0365c0d547d5"
"name": "Development",
"short_name": "DEV",
"description": "Development area",
"updated_at": {
"$date": "2021-06-02T02:03:50.383Z"
},
"created_at": {
"$date": "2021-06-02T02:03:50.383Z"
},
"__v": 0,
"active": true
}
My aggregation
db.checklists.aggregate([
{
"$unwind": "$steps"
},
{
"$unwind": "$steps.areas"
},
{
"$lookup": {
"from": "areas",
"let": {
"area_id": {
"$toObjectId": "$steps.areas.area_id"
}
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$_id",
"$$area_id"
]
}
}
}
],
"as": "convertedItems"
}
},
{
"$group": {
"_id": "$steps.name",
"root": {
"$first": "$$ROOT"
},
"items": {
"$push": {
"$mergeObjects": [
"$steps.areas",
{
"$arrayElemAt": [
"$convertedItems",
0
]
}
]
}
},
}
},
{
"$addFields": {
"values": {
"$reduce": {
"input": "$items",
"initialValue": [],
"in": {
"$concatArrays": [
"$$value",
{
"$cond": [
{
"$in": [
"$$this.area_id",
"$$value.area_id"
]
},
[],
[
"$$this"
]
]
}
]
}
}
}
}
},
{
"$addFields": {
"root.steps.areas": "$values"
}
},
{
"$replaceRoot": {
"newRoot": "$root"
}
},
{
"$group": {
"_id": "$_id",
"root": {
"$first": "$$ROOT"
},
"steps": {
"$push": "$steps"
}
}
},
{
"$addFields": {
"root.steps": "$steps"
}
},
{
"$replaceRoot": {
"newRoot": "$root"
}
},
{
"$project": {
"convertedItems": 0
}
}
])
I don´t get to form this output:
{
"steps": [{
"name": "paso1",
"description": "paso1",
"estimated_time": 50,
"active": true,
"areas": [{
"_id": "60b6e728c44f0365c0d547d6",
"name":"Development",
..... //join or lookup
}, {
"_id": "60b6e7a2c44f0365c0d547d8",
"name":"Development",
..... //join or lookup
}]
}],
"name": "prueba",
"description": "prueba",
"type": "prueba",
"active": true,
"updated_at": {
"$date": "2021-06-02T23:56:02.232Z"
},
"created_at": {
"$date": "2021-06-01T22:44:57.114Z"
},
"__v": 0
}
Thank you very much!
$unwind deconstruct steps array
$lookup with areas collection pass area_id in let
$match to check is _id in area_ids after converting to string
$project to show required fields
$group by _id and reconstruct the steps array and pass your required fields
db.checklists.aggregate([
{ $unwind: "$steps" },
{
$lookup: {
from: "areas",
let: { area_id: "$steps.areas.area_id" },
pipeline: [
{
$match: {
$expr: { $in: [{ $toString: "$_id" }, "$$area_id"] }
}
},
{ $project: { name: 1 } }
],
as: "steps.areas"
}
},
{
$group: {
_id: "$_id",
steps: { $push: "$steps" },
name: { $first: "$name" },
description: { $first: "$description" },
type: { $first: "$type" },
active: { $first: "$active" },
updated_at: { $first: "$updated_at" },
created_at: { $first: "$created_at" },
__v: { $first: "$__v" }
}
}
])
Playground

MongoDB - Returning only one matching array element from Object

One of my project collection.
{
"id": {
"$oid": "5f4600ab7ec81f6c20f8608d"
},
"name": "2",
"category": "2",
"description": "2",
"deadline": "2020-08-10",
"createdBy": {
"$oid": "5f5089a2265ec85b896f848f"
},
"__v": 116,
"todos": [{
"time": {
"$date": "2020-09-10T06:15:36.168Z"
},
"doneAt": {
"$date": "2020-09-10T16:51:53.534Z"
},
"done": true,
"_id": {
"$oid": "5f59c4f53f0095593b2ace25"
},
"user": {
"$oid": "5f3fcbcae5efa018cc3c9b9d"
},
"text": "ooooooo"
}, {
"time": {
"$date": "2020-09-05T13:11:14.139Z"
},
"doneAt": null,
"done": false,
"_id": {
"$oid": "5f538f1eeef4d614830e843c"
},
"user": {
"$oid": "5f5089a2265ec85b896f848f"
},
"text": "11"
}],
"bugs": [{
"time": {
"$date": "2020-09-09T03:30:45.463Z"
},
"fixedAt": null,
"fixed": false,
"_id": {
"$oid": "5f584f152c5530129ff36ea2"
},
"user": {
"$oid": "5f3fcbcae5efa018cc3c9b9d"
},
"text": "new bug"
}, {
"time": {
"$date": "2020-09-06T09:35:12.848Z"
},
"fixedAt": null,
"fixed": false,
"_id": {
"$oid": "5f54e1220297a57896646e98"
},
"user": {
"$oid": "5f3fcbcae5efa018cc3c9b9d"
},
"text": "4"
}, {
"time": {
"$date": "2020-09-06T09:35:12.848Z"
},
"fixedAt": null,
"fixed": false,
"_id": {
"$oid": "5f54e0fc0297a57896646e97"
},
"user": {
"$oid": "5f3fcbcae5efa018cc3c9b9d"
},
"text": "5"
}],
"members": [{
"_id": {
"$oid": "5f57725977ead80a566855e4"
},
"user": {
"$oid": "5f3fcbcae5efa018cc3c9b9d"
}
}, {
"_id": {
"$oid": "5f576452fe05ec34a1bc487b"
},
"user": {
"$oid": "5f4c8654f4b8c6671d5926ac"
}
}, {
"_id": {
"$oid": "5f562a971e16aa39ca290391"
},
"user": {
"$oid": "5f5089a2265ec85b896f848f"
}
}]
}
There are more project collection. I put only one here. My goal is retrive one bug element
such as
{
"time": {
"$date": "2020-09-09T03:30:45.463Z"
},
"fixedAt": null,
"fixed": false,
"_id": {
"$oid": "5f584f152c5530129ff36ea2"
},
"user": {
"$oid": "5f3fcbcae5efa018cc3c9b9d"
},
"text": "new bug"
}
Every project collection and bug element has own id. I just want to retrieve one bug element from an bugs array in project object using mongoose.
Every project object has similar structure.

How to remove duplicate values inside a list array in MongoDB?

I have many records in one collection in MongoDB and this is 3 examples to remove only based one QUESTION match criteria.
{
"_id": {
"$oid": "5f0f561256efe82f5082252e"
},
"Item1": false,
"Item2": "",
"Item3": 1,
"Item4": [
{
"Name": "TYPE",
"Value": "QUESTION"
},
{
"Name": "QUESTION",
"Value": "What is your name?"
},
{
"Name": "CORRECT_ANSWER",
"Value": "1"
},
{
"Name": "ANSWER_1",
"Value": "name one"
},
{
"Name": "ANSWER_2",
"Value": "name two"
}
],
"Item5": [
10
],
"Item6": false
}
and another one to compare
{
"_id": {
"$oid": "5f0f561256efe82f5082252c"
},
"Item1": false,
"Item2": "",
"Item3": 2,
"Item4": [
{
"Name": "TYPE",
"Value": "QUESTION"
},
{
"Name": "QUESTION",
"Value": "What is your name?"
},
{
"Name": "CORRECT_ANSWER",
"Value": "1"
},
{
"Name": "ANSWER_1",
"Value": "name one"
},
{
"Name": "ANSWER_2",
"Value": "name two"
}
],
"Item5": [
10
],
"Item6": false
}
the third one :
{
"_id": {
"$oid": "5f0f561256efe82f5082252d"
},
"Item1": false,
"Item2": "",
"Item3": 3,
"Item4": [
{
"Name": "TYPE",
"Value": "QUESTION"
},
{
"Name": "QUESTION",
"Value": "What is your last name?"
},
{
"Name": "CORRECT_ANSWER",
"Value": "1"
},
{
"Name": "ANSWER_1",
"Value": "name one"
},
{
"Name": "ANSWER_2",
"Value": "name two"
}
],
"Item5": [
10
],
"Item6": false
}
What I'm trying here is to make query with aggregation approach and I only want to focus on Item4 for exactly ("Name": "QUESTION") and the value (the question) for identifying the duplication.
The idea is to looking for duplication in the the question itself only ("What is your name?") in our example here. and I don't want to specify witch question because there are long list of them.
I'm looking just for the duplicated questions no mater what is the question look like.
I used the following approach but still I cannot narrow down the output to be only related to question and its value in order to delete the duplicate in the another step.
db.collections.aggregate([{ $unwind: "$Item4" }, {$group: { _id: { QUESTION: "$Item4.Name.4", Value: "$Item4.Value.4" }}}]).pretty()
I'm executing from mongo shell directly.
The following aggregation will list all the documents (the _ids) which have the duplicates of "Item4.Value" for the condition "Item4.Name": "QUESTION".
db.test.aggregate( [
{
$unwind: "$Item4"
},
{
$match: { "Item4.Name": "QUESTION" }
},
{
$group: {
_id: { "Item4_Value": "$Item4.Value" },
ids: { $push: "$_id" }
}
},
{
$match: { $expr: { $gt: [ { $size: "$ids" }, 1 ] } }
}
] )
It works! thanks a lot. I add it to the rest of code as below :
db.test.find().count()
const duplicatesIds = [];
db.test.aggregate( [
{
$unwind: "$Item4"
},
{
$match: { "Item4.Name": "QUESTION" } //here is the trick...to filter the array to pass only the condition "Item4.Name": "QUESTION".
},
{
$group: {
_id: { "Item4_Value": "$Item4.Value" },
ids: { $push: "$_id" }
}
}
],
{
allowDiskUse: true
}
).forEach(function (doc) {
doc.ids.shift();
doc.ids.forEach(function (dupId) {
duplicatesIds.push(dupId);
})
});
printjson(duplicatesIds);
db.test.remove({_id:{$in:duplicatesIds}})
db.test.find().count()

Resources