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

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.

Related

MongoDB update in array fails: Updating the path 'companies.$.updatedAt' would create a conflict at 'companies.$'

we upgraded (from MongoDB 3.4) to:
MongoDB: 4.2.8
Mongoose: 5.9.10
and now we receive those errors. For the smallest example the models are:
[company.js]
'use strict';
const Schema = require('mongoose').Schema;
module.exports = new Schema({
name: {type: String, required: true},
}, {timestamps: true});
and
[target_group.js]
'use strict';
const Schema = require('mongoose').Schema;
module.exports = new Schema({
title: {
type: String,
required: true,
index: true,
},
minAge: Number,
maxAge: Number,
companies: [Company],
}, {timestamps: true});
and when I try to update the company within a targetgroup
_updateTargetGroup(companyId, company) {
return this.targetGroup.update(
{'companies._id': companyId},
{$set: {'companies.$': company}},
{multi: true});
}
I receive
MongoError: Updating the path 'companies.$.updatedAt' would create a conflict at 'companies.$'
even if I prepend
delete company.updatedAt;
delete company.createdAt;
I get this error.
If I try similar a DB Tool (Robo3T) everything works fine:
db.getCollection('targetgroups').update(
{'companies.name': "Test 1"},
{$set: {'companies.$': {name: "Test 2"}}},
{multi: true});
Of course I could use the workaround
_updateTargetGroup(companyId, company) {
return this.targetGroup.update(
{'companies._id': companyId},
{$set: {'companies.$.name': company.name}},
{multi: true});
}
(this is working in deed), but I'd like to understand the problem and we have also bigger models in the project with same issue.
Is this a problem of the {timestamps: true}? I searched for an explanation but werenot able to find anything ... :-(
The issue originates from using the timestamps as you mentioned but I would not call it a "bug" as in this instance I could argue it's working as intended.
First let's understand what using timestamps does in code, here is a code sample of what mongoose does to an array (company array) with timestamps: (source)
for (let i = 0; i < len; ++i) {
if (updatedAt != null) {
arr[i][updatedAt] = now;
}
if (createdAt != null) {
arr[i][createdAt] = now;
}
}
This runs on every update/insert. As you can see it sets the updatedAt and createdAt of each object in the array meaning the update Object changes from:
{$set: {'companies.$.name': company.name}}
To:
{
"$set": {
"companies.$": company.name,
"updatedAt": "2020-09-22T06:02:11.228Z", //now
"companies.$.updatedAt": "2020-09-22T06:02:11.228Z" //now
},
"$setOnInsert": {
"createdAt": "2020-09-22T06:02:11.228Z" //now
}
}
Now the error occurs when you try to update the same field with two different values/operations, for example if you were to $set and $unset the same field in the same update Mongo does not what to do hence it throws the error.
In your case it happens due to the companies.$.updatedAt field. Because you're updating the entire object at companies.$, that means you are basically setting it to be {name: "Test 2"} this also means you are "deleting" the updatedAt field (amongst others) while mongoose is trying to set it to be it's own value thus causing the error. This is also why your change to companies.$.name works as you would only be setting the name field and not the entire object so there's no conflict created.

MongoDB updating the wrong subdocument in array

I've recently started using MongoDB using Mongoose (from NodeJS), but now I got stuck updating a subdocument in an array.
Let me show you...
I've set up my Restaurant in MongoDB like so:
_id: ObjectId("5edaaed8d8609c2c47fd6582")
name: "Some name"
tables: Array
0: Object
id: ObjectId("5ee277bab0df345e54614b60")
status: "AVAILABLE"
1: Object
id: ObjectId("5ee277bab0df345e54614b61")
status: "AVAILABLE"
As you can see a restaurant can have multiple tables, obviously.
Now I would like to update the status of a table for which I know the _id. I also know the _id of the restaurant that has the table.
But....I only want to update the status if we have the corresponding tableId and this table has the status 'AVAILABLE'.
My update statement:
const result = await Restaurant.updateOne(
{
_id: ObjectId("5edaaed8d8609c2c47fd6582"),
'tables._id': ObjectId("5ee277bab0df345e54614b61"),
'tables.status': 'AVAILABLE'
},
{ $set: { 'tables.$.status': 'CONFIRMED' } }
);
Guess what happens when I run the update-statement above?
It strangely updates the FIRST table (with the wrong table._id)!
However, when I remove the 'tables.status' filter from the query, it does update the right table:
const result = await Restaurant.updateOne(
{
_id: ObjectId("5edaaed8d8609c2c47fd6582"),
'tables._id': ObjectId("5ee277bab0df345e54614b61")
},
{ $set: { 'tables.$.status': 'CONFIRMED' } }
);
Problem here is that I need the status to be 'AVAILABLE', or else it should not update!
Can anybody point me in the wright direction with this?
according to the docs, the positional $ operator acts as a placeholder for the first element that matches the query document
so you are updating only the first array element in the document that matches your query
you should use the filtered positional operator $[identifier]
so your query will be something like that
const result = await Restaurant.updateOne(
{
_id: ObjectId("5edaaed8d8609c2c47fd6582"),
'tables._id': ObjectId("5ee277bab0df345e54614b61"),
'tables.status': 'AVAILABLE'
},
{
$set: { 'tables.$[table].status': 'CONFIRMED' } // update part
},
{
arrayFilters: [{ "table._id": ObjectId("5ee277bab0df345e54614b61"), 'table.status': 'AVAILABLE' }] // options part
}
);
by this way, you're updating the table element that has that tableId and status
hope it helps

Why does MongoDB increase _id by one when inserting with custom id value

I am having some weird issue when inserting a new document with a custom _id value. If i insert a document with a custom _id of 1020716632230383 it gets saved in the database as 1020716632230384. For some reason it is incremented by 1.
what may cause this?
My query is :
exports.register = function(req, res){
console.log('ATTEMPT to register a user with id: ' + req.body.userInfo.id);
// prints 1020716632230383 !!!
var query = {'_id': req.body.userInfo.id};
var update = {
'_id': req.body.userInfo.id ,
'name': req.body.userInfo.name
};
var options = {
upsert: true,
setDefaultsOnInsert : true
};
User.findOneAndUpdate(query, update, options).exec(function(err ,doc){
if(!err){
console.log('REGISTERED!: ' , doc);
res.status(200).json({'success': true, 'doc': doc});
}else{
console.log(err);
res.status(200).json({'success': false});
}
});
};
In my Schema the _id is defined to be a number.
_id: {type:Number}
it is because "setDefaultsOnInsert : true" . when you use this option you can't update "_id" field. it is setting by mongoose.
if you want have a custom "_id" be sure "setDefaultsOnInsert" option is "false".
it is one of problems in mongoose .

MongoDB check if an embedded document is empty?

I have the following document and I need to return name who has no votes
user:
{
'user_id': (id),
'name': (name),
'votes': {
(vote_type): (No.of votes of this type)
},
}
I tried with db.user.find({votes: null},{name:true}).pretty(). No results show up. How do I fix this. Appreciate your help!
By default in MongoDB if a field is unpopulated MongoDB won't create the property for that document so you should be able to test if it $exists:
db.user.find({votes: {$exists: false}},{name: true}).pretty()
If you are setting it to null though you should be able to query for it as you described with checking for null:
db.user.find({votes: null},{name: true}).pretty()
If neither works you might be storing an empty object {} by default and will need to check for that:
db.user.find({votes: {}},{name: true}).pretty()
You can iterate over all the elements and add to those some new field f.e. "withNoVotes".
db.user.find().forEach(function(doc) {
if(doc.votes) {
for(var voteType in votes) {
if(votes[voteType] && votes[voteType] > 0) {
db.user.update({_id: doc._id}, {$set: {withNoVotes: true}});
break;
}
}
}
});
Now find them using usual query to find by this new field
db.user.find({withNoVotes: true});

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 });