Mongodb using addtoSet to ensure there are no duplicates - node.js

Using mongoose against mongodb 2.6 - another issue raised that sounds similar to mine;
https://github.com/LearnBoost/mongoose/issues/1677
I have this piece of code:
$addToSet: {
invite_list: {
$each : [
{ email: 'test#test.com' },
{ email: 'test#test.com' },
{ email: 'test#test.com' },
{ email: 'test#test.com' }]
}
}
which should only store one item but instead its storing 4!
However, changing the query to this
$addToSet: {
invite_list: {
$each : [
'test#test.com',
'test#test.com',
'test#test.com',
'test#test.com' ]
}
}
returns one item, as expected.
Model schema field:
invite_list: [{email: {type: String, trim: true}}],
The query looks like this;
UserModel.findOneAndUpdate(
{
_id: req.params.id,
},
{
$addToSet: {invite_list: { $each : [{ email: 'test#test.com' },
{ email: 'test#test.com' },
{ email: 'test#test.com' },
{ email: 'test#test.com' }] }}
}, function (err, user) {
// morel logic here...
return res.status(200).json(user);
});
http://docs.mongodb.org/manual/reference/operator/update/addToSet/
Is there something Im missing.
Thanks.
J

After reading this, it got me thinking;
Stop Mongoose from creating _id property for sub-document array items
Found a fix;
Model now looks like this;
var subSchema = new mongoose.Schema({email: {type: String, trim: true, _id: false}},{ _id : false })
invite_list: [subSchema],
Works as expected...
J

Related

I have problem updating a subdocument in an array of subdocuments in MongoDB

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.

Mongoose findOneAndUpdate when value is not in array

I have the below Mongoose function where I am trying to do a select WHERE email = req.body.email and referenceId is not in an array.
referenceId is a string array [String] which has an array of object ID's.
const refereeSchema = new Schema({
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
referenceId: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
}
],
token: {
type: String,
required: true
},
password: {
type: String
},
company: {
type: String,
required: true
},
role: {
type: String,
required: false
},
startedOn: Date,
endedOn: Date,
createdAt: Date,
updatedAt: Date
});
Referee.findOneAndUpdate({
email: req.body.email,
referenceId: {
'$ne': [new mongo.ObjectID(req.params.referenceId)]
}
}, {
$push: {
referenceId: new mongo.ObjectID(req.body.referenceId)
}
}, {
new: true
}, (err, doc) => {
if (err) {
res.status(401).send(err);
}
res.send(doc);
});
Each time I run the above it always matches and pushes the ObjectId even though it already existed before.
{ referenceId:
[ 5ba94232b492890982e7a417,
5ba94232b492890982e7a417,
5c20d8f907a55accd720c27f,
5c20d8f907a55accd720c27f,
5c20d8f907a55accd720c27f,
5c20d8f907a55accd720c27f,
5c20d8f907a55accd720c27f,
5c20d8f907a55accd720c27f ],
_id: 5c20e86d72d5c1ce20b961fa,
firstName: 'Richard',
lastName: 'Hendricks',
email: 'hello#domain.com',
token: '319d9f5a-68e1-6f47-a9f1-7fbbf617a45c',
company: 'Pied Piper',
role: '',
__v: 0 }
Could someone please help me?
I'd like to do a check to see if the ObjectId is already in the array then DO NOTHING else push it.
$push will just push data in array, you should use $addToSet
$addToSet only ensures that there are no duplicate items added to the set and does not affect existing duplicate elements. $addToSet does not guarantee a particular ordering of elements in the modified set
more details
Referee.findOneAndUpdate({
email: req.body.email,
referenceId: {
'$ne': [new mongo.ObjectID(req.params.referenceId)]
}
}, {
$addToSet: {
referenceId: new mongo.ObjectID(req.body.referenceId)
}
}, {
new: true
}, (err, doc) => {
if (err) {
res.status(401).send(err);
}
res.send(doc);
});
more ever, you can use $elemMatch or $nin as find query, so that document itself doesnt return if id is in referenceId
$elemMatch :
referenceId: {
'$elemMatch': { '$ne': new mongo.ObjectID(req.params.referenceId) }
}
$nin:
referenceId: {
'$nin': [new mongo.ObjectID(req.params.referenceId)]
}
Consider using the $elemMatch operator in complement of $ne. $ne against the array itself will always return true in this case, because the array you're passing in the parameter is never going to equal the array in the document.
I haven't tested this, but something along these lines should work:
Referee.findOneAndUpdate({
email: req.body.email,
referenceId: {
'$elemMatch': { '$ne': new mongo.ObjectID(req.params.referenceId) }
}
}, {
$push: {
referenceId: new mongo.ObjectID(req.body.referenceId)
}
}, {
new: true
}, (err, doc) => {
if (err) {
res.status(401).send(err);
}
res.send(doc);
});
Also, you probably already know this but there are actually two ObjectId classes you might be importing, the one you want is mongoose.Types.ObjectId

Push values into array with mongoose

I have a problem pushing values into an array with mongoose (yes I have read many topics about it and tried many ways to do it).
So I have this schema
const Postit = new Schema({
text: {
type: String,
required: true
},
status: {
type: String,
default: 'TODO'
},
modified: {
type: Date,
default: Date.now
},
user: {
type: ObjectId,
ref: 'User',
required: true
},
collaborators: [String]
})
And I'm trying to push a string in the collaborators property where the queries match.
So this is the method I use to update it
addCollaborator(uid, pid) {
return Postit.updateOne({
_id: pid,
user: uid
},
{ $push: { collaborators: 'pepe' } },
(err, raw) => {
//TO DO
})
}
But nothing happens. The query match because if I change $push for $set and put a new value to status property for example it updates.
The funny thing is that if I run it in mongodb client terminal it works.
db.postits.updateOne({
_id: ObjectId("5beb1492cf484233f8e21ac1"),
user: ObjectId("5beb1492cf484233f8e21abf")
},
{ $push: {collaborators: 'pepe' }
})
What i'm doing wrong?
Pick promises or callbacks but do not mix them together. Either you do:
addCollaborator(uid, pid) {
Postit.updateOne({
_id: mongoose.Types.ObjectId(pid),
user: uid
},
{ $push: { collaborators: 'pepe' } },
(err, raw) => {
// TO DO
})
}
Or you do:
addCollaborator(uid, pid) {
return Postit.updateOne({
_id: mongoose.Types.ObjectId(pid),
user: uid
},
{ $push: { collaborators: 'pepe' } }).exec()
.then(result => {
// TO DO
})
}
Also make sure your objectId is an actual mongoose.Types.ObjectId
Ok, the problem is I was using an old schema.
The code works perfectly. Enough code for today...

Why $pull does not work when using mongodb on node js [duplicate]

i'm trying to do a pretty simple operation, pull an item from an array with Mongoose on a Mongo database like so:
User.update({ _id: fromUserId }, { $pull: { linkedUsers: [idToDelete] } });
fromUserId & idToDelete are both Objects Ids.
The schema for Users goes like this:
var UserSchema = new Schema({
groups: [],
linkedUsers: [],
name: { type: String, required: true, index: { unique: true } }
});
linkedUsers is an array that only receives Ids of other users.
I've tried this as well:
User.findOne({ _id: fromUserId }, function(err, user) {
user.linkedUsers.pull(idToDelete);
user.save();
});
But with no luck.
The second option seem to almost work when i console the lenghts of the array at different positions but after calling save and checking, the length is still at 36:
User.findOne({ _id: fromUserId }, function(err, user) {
console.log(user.linkedUsers.length); // returns 36
user.linkedUsers.pull(idToDelete);
console.log(user.linkedUsers.length); // returns 35
user.save();
});
So it looks like i'm close but still, no luck. Both Ids are sent via the frontend side of the app.
I'm running those versions:
"mongodb": "^2.2.29",
"mongoose": "^5.0.7",
Thanks in advance.
You need to explicitly define the types in your schema definition i.e.
groups: [{ type: Schema.Types.ObjectId, ref: 'Group' }],
linkedUsers: [{ type: Schema.Types.ObjectId, ref: 'User' }]
and then use either
User.findOneAndUpdate(
{ _id: fromUserId },
{ $pullAll: { linkedUsers: [idToDelete] } },
{ new: true },
function(err, data) {}
);
or
User.findByIdAndUpdate(fromUserId,
{ $pullAll: { linkedUsers: [idToDelete] } },
{ new: true },
function(err, data) {}
);
I had a similar issue. I wanted to delete an object from an array, using the default _id from mongo, but my query was wrong:
const update = { $pull: { cities: cityId }};
It should be:
const update = { $pull: { cities: {_id: cityId} }};

Returning only first element of a sub array with $elemMatch

I'm using node and mongoose, and have a schema that looks like this:
var SubscriberSchema = new Schema({
'user': [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
'level': { type: String, enum: [ 'owner', 'sub', 'commenter', 'poster' ] }
'dateAdded': { type: Date, default: Date.now }
});
// Group Schema
var GroupSchema = new Schema({
'groupOwner': [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
'groupName': String,
'subscribers': [SubscriberSchema],
});
I would like to query the group to find all groups where a user (stored in req.user._id via token authentication) is a subscriber (i.e. their _id is in the subscribers array), and only return the single subscribers array element with their _id.
I've read the Mongo documentation on $elemMatch as this seems to be what I need, and built the query below. This returns the information I want, but returns all elements of the subscribers array. How can I return only the single element of the subscribers array that matches my req.user._id?
Current query, returns all elements of subscribers:
Group
.find( { "subscribers.user": req.user._id}, { subscribers: { $elemMatch: { user: req.user._id }}} )
.sort('groupName')
.populate('groupOwner', 'email firstName lastName')
.populate('subscribers.user', 'email firstName lastName')
.exec(function(err, data) {
if (err) {
logger.error('Unable to retrieve groups for user: ' + err.message);
res.status(500)
} else {
res.json(data);
}
});
This returns the following for subscribers (via util.inspect(data[0].subscribers)):
Subscribers
[{
user:
[ { _id: 1234,
email: 'me#here.com',
firstName: 'Testy',
lastName: 'Testelson' } ] }
user:
[ { _id: 5678,
email: 'you#there.com',
firstName: 'Biggy',
lastName: 'Smalls' } ] }]
Based on the $elemMatch docs, I would assume I would only see user 1234 since that's the record that matches req.user._id. What am I doing wrong here?
Thanks!
In your projection parameter, use the dollar operator:
{"user.$": 1}
This will return a Group with only a single object in its 'subscribers' array.

Resources