Mongoose FindOne returns all children - node.js

Is it ok that findOne returns all subdocument in array? I mean isn't it take a long time to execute that, or is there way to get document with only one subDoc ?
my code :
userModel
.find({
{_id: userId,},
{ contacts: { $elemMatch: { userId: contactId } } }
).select('name contacts');
I cant use userModel.contacts.id() cause I don't have id
How can I add a conversation id to this found document
How can I do
const userData = userModel.findById(userId)
.where('contacts')
.elemMatch({ userId: contactId })
and get the result with only one contact (in order not to search for a contact from the entire array)
and simply change relation like
userData.contacts[0].conversation = conversationId;
await userData.save()

How about this:
db.collection.update({
_id: 1
},
{
$set: {
"contacts.$[x].conversation": 3
}
},
{
arrayFilters: [
{
"x.userId": "John"
}
]
})
playground

Related

Update nested object in array MongoDB

I need to find and update documents with category that corresponding to the query. Array could contain mo than one corresponding id.
Query:
{
"ids": ["61f1cda47018c60012b3dd01", "61f1cdb87018c60012b3dd07"],
"userId": "61eab3e57018c60012b3db3f"
}
I got collection with documents like:
`{
"_id":{"$oid":"61f1cdd07018c60012b3dd09"},
"expenses":[
{"category":"61eafc104b88e154caa58616","price":"1111.00"},
{"category":"61f1cdb87018c60012b3dd07","price":"2222.00"},
{"category":"61f1cda47018c60012b3dd01","price":"1241.00"},
{"category":"61f1cdb87018c60012b3dd07","price":"111.00"}
],
"userId":"61eab3e57018c60012b3db3f"
}`
my method:
async myMethod(ids: [string], userId: string) {
try {
const { ok } = await this.ExpensesModel.updateMany(
{"userId": userId, "expenses.category": { $in: ids }},
{$set: {"expenses.$.category": "newCategoryID"}}
);
return ok
} ........
I path array of ids ["61f1cda47018c60012b3dd01","61f1cdb87018c60012b3dd07","61f1cdb87018c60012b3dd07"] and userId, this code update only 1 category by document.
So can i made it with mongo build in methods? or i need to find matching document and update it it by my self and after that update or insert;
Update with arrayFilters
db.collection.update({
"expenses.category": {
$in: [
"61f1cda47018c60012b3dd01",
"61f1cdb87018c60012b3dd07"
]
}
},
{
$set: {
"expenses.$[elem].category": "61eab3e57018c60012b3db3f"
}
},
{
arrayFilters: [
{
"elem.category": {
$in: [
"61f1cda47018c60012b3dd01",
"61f1cdb87018c60012b3dd07"
]
}
}
]
})
mongoplayground

Mongodb findone document in array by object id

Can I get only 1 photo by objectid? I only need to get 1 Image detail from 1 post by photo but what i get is all photo of post.
this is my db structure
and this is my code looks like:
Post.findOne({
$and: [
{ photo: { $elemMatch: { _id: id } } } ]
}).exec((err, post) => {
if (err) {
return res.status(400).json({ error: err });
}
req.post = post;
console.log(req.post);
next();
});
what i get in req.post is only [].
Thanks in advance
The $elemMatch projection operator provides a way to alter the returned documents, in here coupled with find utilizing second parameter which is projection will achieve that.
Post.find(
{},
{
_id: 0,
photo: { $elemMatch: { _id: id } }
}
);
This will provide leaned documents with the promise: .lean().exec():
Post.find(
{},
{
_id: 0,
photo: { $elemMatch: { _id: id } }
}
)
.lean()
.exec();
NOTE: $elemMatch behaviour is to return the first document where photo's id matches.
You can try with aggregate instead of findOne:
https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/
Post.aggregate([
{ $match: { 'photo._id': id } },
{ $unwind: "$photo" },
{ $match: { 'photo._id': id } },
]);
Maybe not the best, but single photo data is achievable.

Delete a single record from an array of nested document in mongodb

I want to delete just one contact from the contacts array of a particular user
from mongdb dynamically using nodejs
{
_id:ObjectId("532737785878v7676747788"),
firstname:"Mark",
lastname:"Anthony",
email:"xyz#gmail.com",
contacts:[
{
_id:ObjectId("678758478fr7889889)",
firstName:"James",
lastName:"Cole",
phoneNo:"09746"
},
{
_id:ObjectId("678758478fr7889889)"
firstName:"Jane"
lastName:"Doe"
phoneNo:"12345"
}
]
}
I tried this:
User.updateOne(
{email:'xyz#gmail.com', 'contacts._id':678758478fr7889889},
{ $pull : { contacts : { firstName:'Jane', lastName:'Doe', phoneNo:'12345'} } },
{multi:true},
);
I am not getting any error messages and it's not deleting any contact
db.collection.update({
email:'xyz#gmail.com',
contacts: {
$elemMatch: {
"_id": "678758478fr7889889"
}
}
}, {
$pull: {
contacts: {
_id: '678758478fr7889889'
}
}
}
)
Mongoose would use defined schema to create ObjectId's in DB while on writes but it would not use schema on _id(ObjectId's) for find queries, So you've to explicitly say that _id is an ObjectId(). Please have this in your code :
const mongoose = require('mongoose'); // Ignore this if you've already imported.
const ObjectId = mongoose.Types.ObjectId;
// Assuming id is the value you've got in request.
User.updateOne(
{email:'xyz#gmail.com', 'contacts._id':new ObjectId(id)},
{ $pull : { contacts : { firstName:'Jane', lastName:'Doe', phoneNo:'12345'} } });
// you can do the same with `findOneAndUpdate` with options {new: true} which would return updated document, by default it would be false that meant to return old document.
User.findOneAndUpdate(
{email:'xyz#gmail.com', 'contacts._id':new ObjectId(id)},
{ $pull : { contacts : { firstName:'Jane', lastName:'Doe', phoneNo:'12345'} } }, {new : true});

Express Route with /:id input returns empty array

I am trying to have my API take an id as input and return results from mongoDB according to the id given.
My example collection looks like this:
id: 1 {
count: 5
}
id: 2 {
count: 10
}
My mongoose Schemas looks like this:
var tripSchema = new Schema({
_id: Number,
count: Number
},
{collection: 'test'}
);
And I created another file for this route, where I think the error lies in:
module.exports = function(app) {
app.get('/trips/:id', function(req,res) {
console.log(req.params.id); // Does print the ID correctly
var aggr = Trip.aggregate([
{ "$match": {
"_id": {
"$eq": req.params.id
}
}
},
{
"$project": {
"_id" : 1,
"count": "$count"
}
}
])
aggr.options = { allowDiskUse: true };
aggr.exec(function(err, stations){
if(err)
res.send(err);
res.json(stations);
});
});
}
Now using postman I try to GET /trips/72, but this results in an empty array [], there is an entry in the DB for _id 72 with a corresponding count just like above. My question is if this is the correct approach and what I am doing wrong here.
--Update:
There seems to be something wrong with either the match stage or the whole aggregation. I opted for mongoose's findById, and with this it works now:
Trip.findById(req.params.id, function (err, doc){
res.json(doc);
});
req.params.id returns your id in String form, while I think in aggregate match section you need to pass it as ObjectId. So, you should convert it to ObjectId:
$match: { _id: ObjectId(req.params.id) }

Get result as an array instead of documents in mongodb for an attribute

I have a User collection with schema
{
name: String,
books: [
id: { type: Schema.Types.ObjectId, ref: 'Book' } ,
name: String
]
}
Is it possible to get an array of book ids instead of object?
something like:
["53eb797a63ff0e8229b4aca1", "53eb797a63ff0e8229b4aca2", "53eb797a63ff0e8229b4aca3"]
Or
{ids: ["53eb797a63ff0e8229b4aca1", "53eb797a63ff0e8229b4aca2", "53eb797a63ff0e8229b4aca3"]}
and not
{
_id: ObjectId("53eb79d863ff0e8229b97448"),
books:[
{"id" : ObjectId("53eb797a63ff0e8229b4aca1") },
{ "id" : ObjectId("53eb797a63ff0e8229b4acac") },
{ "id" : ObjectId("53eb797a63ff0e8229b4acad") }
]
}
Currently I am doing
User.findOne({}, {"books.id":1} ,function(err, result){
var bookIds = [];
result.books.forEach(function(book){
bookIds.push(book.id);
});
});
Is there any better way?
It could be easily done with Aggregation Pipeline, using $unwind and $group.
db.users.aggregate({
$unwind: '$books'
}, {
$group: {
_id: 'books',
ids: { $addToSet: '$books.id' }
}
})
the same operation using mongoose Model.aggregate() method:
User.aggregate().unwind('$books').group(
_id: 'books',
ids: { $addToSet: '$books.id' }
}).exec(function(err, res) {
// use res[0].ids
})
Note that books here is not a mongoose document, but a plain js object.
You can also add $match to select some part of users collection to run this aggregation query on.
For example, you may select only one particular user:
User.aggregate().match({
_id: uid
}).unwind('$books').group(
_id: 'books',
ids: { $addToSet: '$books.id' }
}).exec(function(err, res) {
// use res[0].ids
})
But if you're not interested in aggregating books from different users into single array, it's best to do it without using $group and $unwind:
User.aggregate().match({
_id: uid
}).project({
_id: 0,
ids: '$books.id'
}).exec(function(err, users) {
// use users[0].ids
})

Resources