Persist rest of the object when updating fields with $set- mongoose - node.js

Below is my collection structure :
{
"_id" : ObjectId("59c9e021af4886c9149a28c7"),
"userkey" : "r1Et2ZNdW",
"devicekey" : "12345",
"analog" : {
"4" : 458,
"6" : 448,
"7" : 100
},
"__v" : 0
}
My update code for the collection is :
DeviceModel.update({_id : device.id}, {$set : {"analog" : analog}}, function(err, doc){
if(err) throw err;
console.log("Device switches updated");
});
I want to update the one (or more) value of "analog" object. I may or may not have entire value of the "analog" collection. Which means most of the time I will be having just {"7" : 200} or {"6" : 500}...etc. If I use this in my above update code it works. But the problem is rest of the items in the object will be removed. That is, the "analog" object will become just {"7" : 200}. What change should I do to my update code to persist the rest of the Object.? or do I need to change my entire logic ?

You need to first flatten the analog variable object into another object with a property that uses the dot notation. This enables the $set operator to correctly update the embedded field denoted in the dot notation without updating/removing/affecting the other fields.
So, in your example you can go about this:
let setObject = {};
Object.keys(analog).forEach(function (key) {
setObject[`analog.${key}`] = analog[key];
});
DeviceModel.update(
{ '_id': device.id },
{ '$set': setObject },
function(err, doc) {
if(err) throw err;
console.log("Device switches updated");
}
);

Related

How to update a collection document using multiple criteria

I have this document in collection "registosSRS":
{
"_id" : ObjectId("5a3a2b47d04b7e07f8a273dc"),
"sessao" : "5",
"valorRelacao" : "4.89",
"valorObjectivo" : "4.97",
"valorAbordagem" : "4.88",
"valorGeral" : "4.92",
"cliente_id" : "5a1407c8099ca208e48170a5",
"email" : "mgoncalves#psi.uminho.pt",
"data" : 1513761607431
}
and this document in collection sessao. I've created this document in another place, and set dadosSRS to {} because I want to later change this value. Is this possible to add this value without having created it?
"_id" : ObjectId("5a3a2b41d04b7e07f8a273db"),
"cliente" : ObjectId("5a1407c8099ca208e48170a5"),
"data" : 1513761601705,
"numero" : "5",
"dadosORS" : ObjectId("5a3a2b41d04b7e07f8a273da"),
"dadosSRS" : {
}
Then I'm looking in collection sessao for a client and number os session as in registosSRS, to add the registosSRS id.
mongoClient.collection('sessao', function(err,collection){
collection.update(
{cliente:result.cliente_id, numero:dadosSRS.sessao},
{$set: {'dadosSRS':dadosSessao.dadosSRS}},
function(result){
if (err) throw err;
console.log(result);
console.log('encontrou registo do cliente na collection registoSRS: ' + result);
});
but the result is null, although I have the client and the session number. What am I doing wrong?
In your callback you have only one argument which is essentially the error object hence the result is null since there is no error thrown from the update option. You need a second argument which returns the actual result object. You are using the wrong update method which returns the result object if the command was executed successfully, not the actual document.
From the documentation, the result object has the following properties:
Name Type Description
result Object The raw result returned from MongoDB, field will vary depending on server version.
Properties
Name Type Description
ok Number Is 1 if the command executed correctly.
n Number The total count of documents scanned.
nModified Number The total count of documents modified.
connection Object The connection object used for the operation.
matchedCount Number The number of documents that matched the filter.
modifiedCount Number The number of documents that were modified.
upsertedCount Number The number of documents upserted.
upsertedId Object The upserted id.
You instead need to use findOneAndUpdate which will return the updated document if found
const { ObjectId } = require('mongodb'); // or ObjectID
// or var ObjectId = require('mongodb').ObjectId if node version < 6
const safeObjectId = s => ObjectId.isValid(s) ? new ObjectId(s) : null;
collection.findOneAndUpdate(
{ 'cliente': safeObjectId(result.cliente_id), 'numero': dadosSRS.sessao },
{ '$set': { 'dadosSRS': dadosSessao.dadosSRS } },
{ 'returnOriginal': true },
function(err, result) { // <-- add result as a second argument in the callback
if (err) throw err;
console.log(result);
console.log('encontrou registo do cliente na collection registoSRS: ' + result);
});

Mongoose throwing You must specify a field error while using a variable at place of an explicit string

My Mongoose Command
Modelname.update(
{$and :[ {'field1' : field1}, {'field2' : field2}, {'field3' : field3}, {'field4' : field4}]},
{
$set : {'timestamp' : timestamp},
$inc : {speed_string: 1}
},{upsert: true}, function (err, record)
{
if (err)
{
}
else
{
}
});
As you can see that speed_string is a variable which is supposed to get increased in the real document or get upserted . I have logged it just before the query and it is indeed a string (and not empty like the error reads) but the query is not going through and I am getting this error.
"errmsg":"'$inc' is empty. You must specify a field like so: {$inc:
{: ...}}"}
When I am replacing that variable with 'expected_string' things are working fine and that is more confusing, nevertheless.
There is something I am doing wrong, help is appreciated.
To use a variable as a field name, you need to use the computed property name syntax where the variable is enclosed in square brackets:
Modelname.update(
{$and :[ {'field1' : field1}, {'field2' : field2}, {'field3' : field3}, {'field4' : field4}]},
{
$set : {'timestamp' : timestamp},
$inc : {[speed_string]: 1}
},{upsert: true}, function (err, record)
{
...
});
Also, multiple terms in a query are implicitly ANDed, so you can simplify your query object to:
Modelname.update(
{
'field1' : field1,
'field2' : field2,
'field3' : field3,
'field4' : field4
},
{
$set : {'timestamp' : timestamp},
$inc : {[speed_string]: 1}
},{upsert: true}, function (err, record)
{
...
});

mongoose $match wont return document

I use two ways to retrieve documents from my collection, the first one:
db.comments.find({"nid" : "req.body.data"});
returns many doc like:
{
"nid" : 20404,
"_id" : ObjectId("5638ba331294943d3d0a092b"),
"uid" : 1937,
"posted" : ISODate("2015-11-03T13:44:19.811Z"),
"text" : "txt",
"title" : "Test nid 2",
"stars" : 3,
"__v" : 0
}
,
And for another query I need to use aggregate and the query:
var pipleline = [
{$match: {nid:req.body.data}}
];
Comments.aggregate(pipleline, function(err, rank){
if(err) {
res.send("Error", String(err));
}
res.send(rank);
});
Returns [] - empty array.
Any ideas?
You can use the built in function chaining mongoose provides. Aside from match, it also has sort, project, group, and few others I don't know off the top of my head. More info here
Comments.aggregate().match({nid:req.body.data})
.exec(function(err,rank){
if(err) {
res.send("Error", String(err));
}
res.send(rank);
});

Filter subdocument and trigger

i have collection of objects inside an invitation, having hard time to filter particular object and trigger it's boolean field.
Document:
"Invitation" : [
{
"__v" : 0,
"userID" : ObjectId("54afaabd88694dc019d3b628"),//ObjectId of personA
"__t" : "USER",
"_id" : ObjectId("54b5022b583973580c706784"),
"Accepted" : false
},
{
"__v" : 0,
"userID" : ObjectId("54af6ce091324fd00f97a15f"),//ObjectId of personB
"__t" : "USER",
"_id" : ObjectId("54bde39cdd55dd9016271f14"),
"Accepted" : false
}
]
here i have only two objects inside Invitation array,it can be more than two.
Let's say personA and personB send me Invitation, so two different invitation objects are inserted into database having different fields, with objectId of both persons(userID in above document), now if i accept only invitation of personA, it should trigger accepted field of personA object only, here is what i tried so far, but not working as per expectation.
Controller:
User.find({_id: req.user._id},'Invitation',function(err,docs) {
if (err) {
console.log(err);
}
var results = [];
async.each(docs,function(doc,callback) {
async.each(doc.Invitation,function(invite,callback) {
User.findOneAndUpdate(
{'_id': doc._id, 'Invitation._id': invite._id},
{'$set': {'Invitation.$.Accepted': !invite.Accepted}},
function(err,doc) {
results.push(doc);
callback(err);
}
);
},callback);
},function(err) {
if (err)
console.log(err);
console.log('end'+results);
});
});
finally i am looking for a query which can be used to filter a single element or object, like if i accept invitation of personA then Accepted field of personA object should be set to true.
i would be really helpful if some logic is provided.
Thank you
Not a very clear question. But it seems all you really need to do here is just match the only sub-document you want to update in the first place:
User.find(
{
"_id": "req.user._id",
"Invitation._id": personA.id
},
{ "Invitation.$": 1 },
function(err,docs) {
// and continue
}
);
This is the form of the positional $ operator in a "projection" context. Where only the "singular" matched element is returned.
Once you have a "singular" result, then all the other code works as designed.
I should know after all because I wrote it for you. Not that you are paying any decent respect to that.
Update on Aggregate in Mongodb
Toggle boolean value of subdocuments
Or personA.userID or whatever makes it work.
Just use the unique identifier for the "user" where you expect that to match the query conditions.
You can do this:
db.user.update({"invitation.userID": 1}, {"$set" : {"invitation.$.Accepted" : true}});
Replacing the value 1 with the user ID you want to update.
The code is in the syntax of MongoShell, simply convert to driver syntax you are using
The operator used was the $. According to the documentation: The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array. To project, or return, an array element from a read operation, see the $ projection operator.
For more details see: http://docs.mongodb.org/manual/reference/operator/update/positional/

Multiple updates in one document using mongoose

I want to update multiple values in a single document in one mongoose call. Is this possible?
I have something similar to this:
update = {$inc : { numShown : 1 }, $inc : { secondField.subField : 1 }};
options = {};
MyModel.findByIdAndUpdate(req._id, update, options, function(err){
if(err){ return console.error(err);}
}
It runs, but doesn't update anything.
You need to combine the two $inc values into a single object and quote the dotted key:
update = { $inc : { numShown : 1, 'secondField.subField' : 1 } };

Resources