adding multiple same documents using addtoset command in mongoose - mongodb

My schema is
var UserQuizSchema = mongoose.Schema({
uid:{type:ObjectId,required: true,index:true},
answer:[{content:String, qid:ObjectId ,time:Date }],
});
In this schema, 'uid' represents user identifier, while the 'answer' array stores the answers the student had answered. in each answer, qid relates to the question ID, and 'content' is the student's real answer, 'time' is the modified time stamp for the answer.
Here I use mongoose to upsert the new answers into the array
function updateAnswer(uid,question_id,answer,callback){
var options = { new: false };
var quiz_id = mongoose.Types.ObjectId(quiz_id);
var qid = mongoose.Types.ObjectId(question_id);
UserQuizModel.findOneAndUpdate({'uid':uid},{'$addToSet':{'answer':{'qid':qid, 'content':answer} } },options,function(err,ref){
if(err) {
console.log('update '.red,err);
callback(err, null);
}else{
console.log('update '.green+ref);
callback(null,ref);
}
})
}
In the common sense, by using addToSet command, the element in the answer array should be unique, but in my example, the answer array could have multiple same embedded documents only except each embedded document has one unique OjbectId _id
such as
answer:
[ { qid: 5175aecf0e5b061414000001, _id: 518a5e5895fc9ddc1e000003 },
{ qid: 5175aecf0e5b061414000001, _id: 518a5e5f95fc9ddc1e000004 } ] }
you see the qid of two embedded documents are the same, but _id are different.
Why there is a additional _id, I don't put it the schema design ??

You can disable the _id in your embedded objects by explicitly defining a schema for the elements with the _id option set to false:
var UserQuizSchema = mongoose.Schema({
uid:{type:ObjectId,required: true,index:true},
answer:[new Schema({content:String, qid:ObjectId, time:Date}, {_id:false})]
});

Related

Insert if data doesn’t exist in the document – How to do insert new field and data with Mongoose?

I want to insert to new field and data in existing document, if there is no data in it.
if (repoData.detailViewCounter == undefined) {
console.log('create 0')
Repo.findOneAndUpdate({
'owner.login': userId,
'name': pageId
}, {
detailViewCounter: 0
},
{new: true, upsert: true})
}
So When condition like this, and there is no detailViewCounter field, insert new field with number 0.
{
'owner.login': userId,
'name': pageId
}
But When I run this code and check the MongoDB Compass, there is still no data.
And I also create repo schema detailViewCounter with type:Number
change in option upsert: false
one major problem is field name "owner.login" this is causing the issue, this will search in object owner: { login: userId } but in actual we have string "owner.login", so this is not able to find any matching document, and this will not update record.
you can check after removing condition on field owner.login, it will work
Repo.findOneAndUpdate({
'name': pageId
},
{ detailViewCounter: 0 },
{ new: true, upsert: false }
)
Look at MongoDB restriction on field names, and good question in SO.

How to guarantee unique primary key with one update query

In my Movie schema, I have a field "release_date" who can contain nested subdocuments.
These subdocuments contains three fields :
country_code
date
details
I need to guarantee the first two fields are unique (primary key).
I first tried to set a unique index. But I finally realized that MongoDB does not support unique indexes on subdocuments.
Index is created, but validation does not trigger, and I can still add duplicates.
Then, I tried to modify my update function to prevent duplicates, as explained in this article (see Workarounds) : http://joegornick.com/2012/10/25/mongodb-unique-indexes-on-single-embedded-documents/
$ne works well but in my case, I have a combination of two fields, and it's a way more complicated...
$addToSet is nice, but not exactly what I am searching for, because "details" field can be not unique.
I also tried plugin like mongoose-unique-validator, but it does not work with subdocuments ...
I finally ended up with two queries. One for searching existing subdocument, another to add a subdocument if the previous query returns no document.
insertReleaseDate: async(root, args) => {
const { movieId, fields } = args
// Searching for an existing primary key
const document = await Movie.find(
{
_id: movieId,
release_date: {
$elemMatch: {
country_code: fields.country_code,
date: fields.date
}
}
}
)
if (document.length > 0) {
throw new Error('Duplicate error')
}
// Updating the document
const response = await Movie.updateOne(
{ _id: movieId },
{ $push: { release_date: fields } }
)
return response
}
This code works fine, but I would have preferred to use only one query.
Any idea ? I don't understand why it's so complicated as it should be a common usage.
Thanks RichieK for your answer ! It's working great.
Just take care to put the field name before "$not" like this :
insertReleaseDate: async(root, args) => {
const { movieId, fields } = args
const response = await Movie.updateOne(
{
_id: movieId,
release_date: {
$not: {
$elemMatch: {
country_code: fields.country_code,
date: fields.date
}
}
}
},
{ $push: { release_date: fields } }
)
return formatResponse(response, movieId)
}
Thanks a lot !

Meteor/Mongo - add/update element in sub array dynamically

So I have found quite few related posts on SO on how to update a field in a sub array, such as this one here
What I want to achieve is basically the same thing, but updating a field in a subarray dynamically, instead of just calling the field name in the query.
Now I also found how to do that straight in the main object, but cant seem to do it in the sub array.
Code to insert dynamically in sub-object:
_.each(data.data, function(val, key) {
var obj = {};
obj['general.'+key] = val;
insert = 0 || (Documents.update(
{ _id: data._id },
{ $set: obj}
));
});
Here is the tree of what I am trying to do:
Documents: {
_id: '123123'
...
smallRoom:
[
_id: '456456'
name: 'name1'
description: 'description1'
],
[
...
]
}
Here is my code:
// insert a new object in smallRoom, with only the _id so far
var newID = new Mongo.ObjectID;
var createId = {_id: newID._str};
Documents.update({_id: data._id},{$push:{smallRooms: createId}})
And the part to insert the other fields:
_.each(data.data, function(val, key) {
var obj = {};
obj['simpleRoom.$'+key] = val;
console.log(Documents.update(
{
_id: data._id, <<== the document id that I want to update
smallRoom: {
$elemMatch:{
_id : newID._str, <<== the smallRoom id that I want to update
}
}
},
{
$set: obj
}
));
});
Ok, having said that, I understand I can insert the whole object straight away, not having to push every single field.
But I guess this question is more like, how does it work if smallRoom had 50 fields, and I want to update 3 random fields ? (So I would NEED to use the _each loop as I wouldnt know in advance which field to update, and would not want to replace the whole object)
I'm not sure I 100% understand your question, but I think the answer to what you are asking is to use the $ symbol.
Example:
Documents.update(
{
_id: data._id, smallRoom._id: newID._str
},
{
$set: { smallroom.$.name: 'new name' }
}
);
You are finding the document that matches the _id: data._id, then finding the object in the array smallRoom that has an _id equal to newId._str. Then you are using the $ sign to tell Mongo to update that object's name key.
Hope that helps

Update nested array object (put request)

I have an array inside a document of a collection called pown.
{
_id: 123..,
name: pupies,
pups:[ {name: pup1, location: somewhere}, {name: pup2, ...}]
}
Now a user using my rest-service sends the entire first entry as put request:
{name: pup1, location: inTown}
After that I want to update this element in my database.
Therefore I tried this:
var updatedPup = req.body;
var searchQuery = {
_id : 123...,
pups : { name : req.body.name }
}
var updateQuery = {
$set: {'pups': updatedPup }
}
db.pown.update(searchQuery, updateQuery, function(err, data){ ... }
Unfortunately it is not updating anythig.
Does anyone know how to update an entire array-element?
As Neil pointed, you need to be acquainted with the dot notation(used to select the fields) and the positional operator $ (used to select a particular element in an array i.e the element matched in the original search query). If you want to replace the whole element in the array
var updateQuery= {
"$set":{"pups.$": updatedPup}
}
If you only need to change the location,
var updateQuery= {
"$set":{"pups.$.location": updatedPup.location}
}
The problem here is that the selection in your query actually wants to update an embedded array element in your document. The first thing is that you want to use "dot notation" instead, and then you also want the positional $ modifier to select the correct element:
db.pown.update(
{ "pups.name": req.body.name },
{ "$set": { "pups.$.locatation": req.body.location }
)
That would be the nice way to do things. Mostly because you really only want to modify the "location" property of the sub-document. So that is how you express that.

Mongo: find items that don't have a certain field

How to search for documents in a collection that are missing a certain field in MongoDB?
Yeah, it's possible using $exists:
db.things.find( { a : { $exists : false } } ); // return if a is missing
When is true, $exists matches the documents that contain the field, including documents where the field value is null. If is false, the query returns only the documents that do not contain the field.
If you don't care if the field is missing or null (or if it's never null) then you can use the slightly shorter and safer:
db.things.find( { a : null } ); // return if a is missing or null
It's safer because $exists will return true even if the field is null, which often is not the desired result and can lead to an NPE.
just for the reference here, for those of you using mongoose (v6) and trying to use the $exists to find a field that is not defined in your mongoose schema, mongoose v6 will escape it.
see here https://mongoosejs.com/docs/migrating_to_6.html#strictquery-is-removed-and-replaced-by-strict
for example:
const userSchema = new Schema({ name: String });
const User = mongoose.model('User', userSchema);
// By default, this is equivalent to `User.find()` because Mongoose filters out `notInSchema`
await User.find({ notInSchema: 1 });
// Set `strictQuery: false` to opt in to filtering by properties that aren't in the schema
await User.find({ notInSchema: 1 }, null, { strictQuery: false });
// equivalent:
await User.find({ notInSchema: 1 }).setOptions({ strictQuery: false });