I have a user schema and users can be friends.
I have a friendship schema:
var friendshipSchema = mongoose.Schema({
from : { type: Schema.Types.ObjectId, ref: 'User' },
to : { type: Schema.Types.ObjectId, ref: 'User' }
});
Now, I want to get all friends from an user foo:
Friendship
.find({
$or : [
{ from : foo._id },
{ to : foo._id }
]
})
The problem is: I want to populate all friends from foo, but not foo himself (since for X friends I would have X foos populated).
How could I achieve that?
Thanks!
This is what I did:
Friendship
.find({
$or : [
{ from : user._id },
{ to : user._id }
]
})
.populate({
path: 'from',
match: { _id: { $ne: user._id }},
select: '-token'
},{
path: 'to',
match: { _id: { $ne: user._id }},
select: '-token'
})
.lean()
.exec(function(err, retData)
{
// Do something here...
});
Related
I have problems updating a subdocument in an array of subdocuments.
Here is my data structure in the users collection:
{
favorites: [
{
id: new ObjectId("639707f36bf9468265d91810"),
expiresAt: 1671361200000,
reminder: false
},
{
id: new ObjectId("637cc4c986b4fbec43579e1f"),
expiresAt: 1672603200000,
reminder: false
}
],
_id: new ObjectId("637e8af40e43f40373686da2"),
email: 'something#something.com',
forename: 'something',
surname: 'something',
role: 'user',
password: 'something',
__v: 0
}
My Schema is:
const userSchema = new Schema({
email: String,
forename: String,
surname: String,
role: String,
password: String,
favorites: {
id: { type: Schema.Types.ObjectId, ref: "Event" },
expiresAt: Number,
reminder: Boolean,
},
});
I want to update the reminder field in a subdocument based on the subdocument’s id.
I’ve tried following approaches:
1.
User.findOneAndUpdate(
{ _id: req.body.user, "favorites.id": { $eq: BSON.ObjectId(req.body.id) } },
{ $set: { "favorites.$.reminder": true } },
).setOptions({ sanitizeFilter: true });
Here nothing happens. It finds the document but does not update it.
2.
User.findOneAndUpdate(
{ _id: req.body.user },
{ $set: { "favorites.$[elem].reminder": true } },
{
arrayFilters: [{ "elem.id": { $eq: BSON.ObjectId(req.body.id) } }],
returnNewDocument: true,
}
).setOptions({ sanitizeFilter: true });
Here it returns an error: “Error: Could not find path “favorites.0.id” in schema”
I cannot find where is my mistake? Any help is much appreciated!
P.S.
Mongo version is 5.0.14
Try to use updateMany instead.
User.updateMany(
{
_id: userId,
"favorites.id": eventId
},
{
$set: {
"favorites.$.reminder": true
}
},
function(err, res) {
if (err) {
// Handle error
} else {
// Handle success
}
}
);
I think you can adapt the query to your calling method findOneAndUpdate. But it's enough to you.
Here's my Mongoose schema:
const productSchema = mongoose.Schema({
writer: {
type: Schema.Types.ObjectId,
ref: 'User'
},
schedule: [{
dateTime: {
type: String,
unique: 1
},
candidates: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
attorn: {
type: Schema.Types.ObjectId,
ref: 'User'
}
}]
...
This is where I get my items along with the candidates:
Product.find({ '_id': { $in: productIds } })
.populate('writer')
.populate({
path: 'schedule',
populate: {
path: 'candidates',
model: 'User'
}
})
.exec((err, product) => {
if (err) return res.status(400).send(err)
return res.status(200).send(product)
})
Result of this code is a product with only populated 'writer' and empty array for 'candidates'
If I remove the line for populating 'candidates', it returns array of _id for 'candidates'
How do I populate the 'candidates' array?
can you try this
Product.find({ _id: { $in: productIds } })
.populate("writer")
.populate({
path: "schedule.candidates",
})
.populate({
path: "schedule.attorn",
})
.exec((err, product) => {
if (err) return res.status(400).send(err);
return res.status(200).send(product);
});
My chat object has got an array with two elements - users id. I have first id from url params, but second id I have in array of users. Now I want to get all chats where first id is this one from url, and second is in the array. I think that example how I tried to do it will be the best explanation of this problem :
Chat.find({
users: { $all: [req.params.id, { $in: usersIdArray }] }
})
where usersIdArray is for example:
[ 5f8777a01d8c122e74cb6f08, 5f8777721d8c122e74cb6f02 ]
they are numbers, not strings. I don't know if it is important...
The error I get now :
(node:12168) UnhandledPromiseRejectionWarning: CastError: Cast to ObjectId failed for value "{ '$in': [ 5f8777a01d8c122e74cb6f08, 5f8777721d8c122e74cb6f02 ] }" at path "users" for model "Chat"
And my chat schema:
// Create schema
const ChatSchema = new Schema({
users: {
type: [{
type: Schema.Types.ObjectId,
ref: 'Users',
}, {
type: Schema.Types.ObjectId,
ref: 'Users',
}],
required: [true, 'Podaj uczestników czatu.'],
},
lastMessage: {
type: Schema.Types.ObjectId,
ref: 'Message'
}
}, { timestamps: opts });
Since the length of your array is fixed (2), you can just query based on array position:
Chat.find({
"users.0": req.params.id,
"users.1": {$in: usersIdArray}
});
If that doesn't work then probably because usersIdArray are actually not ObjectIds, in which case you'd need to map them:
usersIdArray.map(x => ObjectId(x))
#Christian Fritz, I had to add $or to your solution and everything is fine now:
Chat.find({
$or: [
{
"users.1": req.params.id,
"users.0": { $in: usersIdArray }
}, {
"users.0": req.params.id,
"users.1": { $in: usersIdArray }
}]
})
So the issue that I'm having is that I want to like user comment only one time, currently im using addToSet operator, since by definition it doesn't add value if that value is already present.
But in my case it adds, probably because I am adding object instead of value and when I add mongo generates _id?
This is my event model:
creator: {
type: Schema.Types.ObjectId,
ref: 'User'
},
comments: [
{
user: {
type: Schema.Types.ObjectId,
ref: 'User'
},
text: {
type: String,
required: true
},
likes: [
{
user: {
type: Schema.Types.ObjectId,
ref: 'User'
}
}
]
}
]
}
And this is my addLike function:
commentLike: async (req, res) => {
console.log('working', req.params.id, req.params.idas, req.params.commentID);
Events.findOneAndUpdate(
{ _id: req.params.idas, comments: { $elemMatch: { _id: req.params.commentID } } },
{ $addToSet: { 'comments.$.likes': { user: req.params.id } } },
(result) => {
res.json(result);
}
);
}
My likes array looks like this when I add like:
"likes" : [
{
"user" : ObjectId("5b53442c1f09f525441dac31"),
"_id" : ObjectId("5b54a5263e65324504035eac")
},
{
"user" : ObjectId("5b4b4725a3a5583ba8f8d513"),
"_id" : ObjectId("5b54a5bb3e65324504035eb0")
},
]
You can follow this
db.collection.update(
{ "_id": req.params.idas, "comments": { "$elemMatch": { "_id": req.params.commentID, "likes.user": { "$ne": req.params.id } } } },
{ "$push": { "comments.$.likes": { "user": req.params.id } } }
})
And if you just started with your project then you should follow JohnnyHK opinion and make your array some thing like this to make $addToSet workable
likes: [{ type: Schema.Types.ObjectId, ref: 'User' }]
Another option is to change your schema definition to add "_id: false" to prevent the _id field from being generated for subdocs in the array.
In this case, $addToSet will work as you expect, even with nested subdocs as long as your doc exactly matches.
comments: [
{
user: {
type: Schema.Types.ObjectId,
ref: 'User'
},
text: {
type: String,
required: true
},
likes: [
{
_id: false, //add this
user: {
type: Schema.Types.ObjectId,
ref: 'User'
}
}
]
}
]
I'm using nodejs/express and mongoose. However, when trying to use .populate(...) on a document it doesn't work. Here is my code:
I have this schema for a User:
const UserSchema = new Schema({
...
friends: [{
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
...
}],
sentRequests: [{
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
...
}],
recievedRequests: [{
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
...
}]
}
mongoose.model('users', UserSchema);
Then when I try to show all the recievedRequests to a page I use this code:
router.get('/friendRequests', (req, res) => {
User.findOne({
_id: req.user.id // where req.user is the currently logged in user
})
.populate('recievedRequests.user')
.then(curUser => {
res.render('friends/friendRequests', {
curUser: curUser
});
})
});
where User is the model and req.user.id is the id of the currently logged in user. However, in the webpage whenever I reference curUser.recievedRequests.user it simply returns the id of the user and not the actual user object. Does anyone know what I might be doing wrong here?
Thanks.
EDIT - Example
For a document:
user1 = {
_id: ObjectId("5ae94b0b2bb9383d4029d64b"),
...
recievedRequests: [
{"_id" : ObjectId("5ae94b5f29c86c4a343d2d0a") }
]
}
and
user2 = {
_id: ObjectId("5ae94b5f29c86c4a343d2d0a"),
...
}
Using my above code on this document:
router.get('/friendRequests', (req, res) => {
User.findOne({
_id: user1.id
})
.populate('recievedRequests.user')
.then(curUser => {
res.render('friends/friendRequests', {
curUser: curUser
});
})
});
results in curUser.recievedRequests = [{ _id : 5ae94b5f29c86c4a343d2d0a }]