Delete whole document for matching objectId in array of objects - MongoDB - node.js

The structure of my document looks like this in MongoDB :-
{
_id : "7464bbuiecdhbjdje"
client : "MJMK"
users : [
{_id : "1234" , name : "first user"}
]
}
I would like to remove the whole document for matching users._id. In this case, for a user with _id 1234,the whole document needs to be removed. I have been unable to find any efficient function that does this in Node using mongoose. Thanks for your help.

You want to use deleteMany.
This will delete all documents containing the matches query, in this case a user with matching _id in the users array. Our query will be utilizing Mongo's dot notation to access the array like so:
Model.deleteMany({ 'users._id': "1234" })

From the shell following works. Isn't it possible to do the same in node?
db.collectionname.find({ "users": { $elemMatch: { "_id": "1234" } } })
db.collectionname.remove({ "users": { $elemMatch: { "_id": "1234" } } })

Try this:
db.collectionName.remove({"users._id":{_id:"1234"}})

Related

Update a value inside array of objects, inside a document in MongoDB

Here is what my collection looks like
Now suppose I have to update count of 2nd document whose reportTypes.reasonId is 300. I have access to _id as well as reasonId to update the count. I am using Mongoose to query things in my Node application.
What can I try to solve this?
You can do it via arrayFilters:
db.collection.update(
{
managerId:3
},
{
$inc:{"reportTypes.$[x].count":1}
},
{
arrayFilters:[{"x.reasonId":300 }]
}
)
playground
Explained:
Specify the matching document in the query part and create arrayFilter "x" matching the correct reportTYpes array subdocument , in the update part use the $inc operation to increment the count value in the example with 1
you should use dot notation and the $ update operator to do this:
(I'm assuming your collection is called Reason)
var conditions = {
'_id': '6244........',
'reasonTypes.reasonId': 300
}
var update = {
$inc: {
'reasonTypes.$.count': 1
}
};
Reason.update(conditions, update, function(err) {
// Handle error here
})
You can find more on the operator here mongo $ update operator

Mongoose query for ID using an array

I want to query MongoDB using Mongoose with an array of objectIDs. I am not sure how to do this and also get back the 'name' property that is on the root of each document. I've looked online and can't seem to get it working. Below are two things I have tried that might be close but don't work.
{ _id: {
$in : ['5d193a4826540f7a89757f1d']
}
}
{ "name" : {
id: {
$in : ['5d193a4826540f7a89757f1d', '5d8c104d0f867b753d1f506c']
}
}}
First make ObjectIds from your strings:
const ids = ['5d193a4826540f7a89757f1d', '5d8c104d0f867b753d1f506c'];
const queryIds = ids.map(item => ObjectId(item));
Then find needed documents of your collection and set name: 1 to get only name (and _id by default):
collection.find({"_id": {$in : queryIds}}, {name: 1});

$push on the basis $cond in mongodb?

I have Review schema having likeBy array contains the id of the users who likes the review...
I have to query it like
case 1: If id exist the pull it.
case 2: If id does not exist then push it.
Is it possible to do in a single query???
Review Schema
{
"_id" : ObjectId("5aa0f4aeb5cbb2313276cccc"),
"__v" : 0,
"likeBy" : [ ]
}
This is how I would have done it
db.people.findAndModify({
query: { likedBy : {idOftheUserinLikedArray: "anyId" }},
$push: { likedBy : {idOftheUserinLikedArray: "anyId" } },
new : true
})
I am not sure if that's what you want. Let me know if that works for you.
What this will do is, if it finds that Review object against the property your are searching for in likedBy array property of the documents it will return you the updated object so you can check the likedBy array if the value is inserted or not.
May be helpful
And don't forget to give it a look

mongoose exclude field in array

I have something like:
Schema Subdocument
name: String
data: Mixed
Schema Stuff
documents: [Subdocument]
Now, in my API there are two endpoints, one for the Subdocument and another for Stuff. When I want to get a Subdocument I need to contain the data field, but when I want to get Stuff, I want to show the name of those subdocuments, but I don't want to show the data field because is quite large and it won't be used.
So, to keep things clear, data is not private. It's just that I don't want it to be shown when I get it from Stuff
I tried by doing:
Stuff.findById(id)
.populate("documents")
.populate("-documents.data")
but that doesn't work... I'm getting the Stuffwith the Subdocumentcontaining the name and data. I feel like i'm missing to tell mongoose when I call populate("-documents.data") that documents is an array and I want to exclude the data field for each element in this array.
edit: Sorry the Schema I provided was not for my case. In my case it was not embedded, but a reference, like so:
Schema Subdocument
name: String
data: Mixed
Schema Stuff
documents: [{
type: Schema.Types.ObjectId,
ref: 'Subdocument'
}]
Assuming subDocument is not embedded and using as "ref" as you say populate is working but data part is not included:
Stuff.findById(id).populate( { "path" : "documents", "select" : "-data" })
Your documents have an "embedded" schema, so populate is not used here, it is used only for "referenced" schemas where the other objects are in another collection.
Fortunately with "embedded" there is an easy way using projection:
Stuff.findById(id,{ "documents.name": 1 },function(err,results) {
})
With results like
{ "documents": [{ "name": "this" },{ "name": "that" }] }
Or with .aggregate() and the $map operator:
Stuff.aggregate(
[
{ "$match": { "_id": ObjectID(id) } },
{ "$project": {
"documents": {
"$map": {
"$input": "$documents",
"as": "el",
"in": "$$el.name"
}
}
}}
],function(err,results) {
}
)
That will just tranform into an array of "only" the name "values", which is different to the last form.
{ "documents": ["this", "that"] }
Note, if using .aggregate() you need to properly cast the ObjectId as the autocasting from mongoose schema types does not work in aggregation pipeline stages.

Mongodb querying from part of object in array

Here's the sample document I'm trying to query
{
"_id":"asdf0-002f-42d6-b111-ade91df09249",
"user":[
{
"_id":"14bfgdsfg0-3708-46ee-8164-7ee1d029a507",
"n":"aaa"
},
{
"_id":"aasdfa89-5cfe-4861-8a9a-f77428158ca9",
"n":"bbb"
}
]
}
The document has 2 user references and contains the user _id and other misc information. I have the 2 user ids and am trying to get this document via only the user ids. I also don't know the order of the 2 ids. Is this a possible query?
col.findOne({
user:{
$all:[
{
_id:"14bfgdsfg0-3708-46ee-8164-7ee1d029a507"
},
{
_id:"aasdfa89-5cfe-4861-8a9a-f77428158ca9"
}
]
}
})
^^ Something that I've tried that doesn't work.
You are close with your $all attempt.
col.findOne({
"user._id":{
$all : [ "14bfgdsfg0-3708-46ee-8164-7ee1d029a507",
"aasdfa89-5cfe-4861-8a9a-f77428158ca9" ]
}
}
You can query a sub-document by wrapping it quotes. From there $all works against the values you are looking for.
Mongodb find a document with all subdocuments satisfying a condition shows a variation on this type of query.
ElemMatch should do the trick.
col.findOne({user:{$elemMatch:{"_id":"14bfgdsfg0-3708-46ee-8164-7ee1d029a507", "_id":"aasdfa89-5cfe-4861-8a9a-f77428158ca9" }}})

Resources