Mongodb $inc embedded value syntax - javascript

I'm trying to increment a field in my mongodb document using the $inc operator. The field I am trying to increment is a sub-property of my document's count field, e.g.:
mydoc: {
count: {
schedules: 0
}
}
When I try this:
> db.mydocs.update({ _id: new ObjectId('4db5c2f3dc73c5afdaffd636') }, { $inc: { count.schedules: 1 } }, { upsert: true, safe: true }, null);
from my mongo shell, I get this error message:
Mon Apr 25 11:59:05 SyntaxError: missing : after property id (shell):1
I've tried several syntax variations with similar results. Do I need to take a different approach to this? I've verified my document exists and has a count.schedules field that is set to 0.
I can directly set the value using a command like this:
db.mydocs.update({ _id: new ObjectId('4db5c2f3dc73c5afdaffd636') }, { $set: { count: { schedules:1 } } }, null, null);
But if I try that syntax for the $inc operation, I get this error:
Modifier $inc allowed for numbers only
Thanks

Yes, you can do a $inc only on numbers. Here is how I tried to reproduce your problem, you can notice I've used proper quotes, which is the reason you are seeing the missing : after property id(shell):1 error.
> db.schedules.save({"mydoc": { "count": { "schedules": 0}}});
> db.schedules.find();
{ "_id" : ObjectId("4db5cf199e631c2a52a7c643"), "mydoc" : { "count" : { "schedules" : 0 } } }
> db.schedules.update({ _id: new ObjectId("4db5cf199e631c2a52a7c643") }, { $inc: { "mydoc.count.schedules": 1 } }, { upsert: true, safe: true }, null);
> db.schedules.find();
{ "_id" : ObjectId("4db5cf199e631c2a52a7c643"), "mydoc" : { "count" : { "schedules" : 1 } } }
Hope this helps!

I think this may be a simple fix. Try putting quotes around count.schedules like so:
db.mydocs.update({ _id: new ObjectId('4db5c2f3dc73c5afdaffd636') }, { $inc: { 'count.schedules': 1 } }, { upsert: true, safe: true }, null);

Related

Node.js Mongodb toggling boolean operator doesnt work

Hello Im having a weird issue
Ive looked in a few previous questions but ive ran into an issue
Basically I have a document containing a boolean
This boolean is called enabled
Id like to switch it using the findOneAndUpdate function
{ $set: { enabled: { $not: "$enabled" } } }
This is what ive come to according to previous questions
However when I attempt it this is the result
enabled: { '$not': '$enabled' }
Here is my full code
db.findOneAndUpdate({
_id: "Sample"
}, {
$set: {
enabled: {
$not: "$enabled"
}
}
}, {
new: true
}, function(err, result) {})
The problem is that you are trying to use the $not aggregation operator inside of a legacy update.
In order to use aggregation operators you will need to use Updates with Aggregation Pipeline.
For your example, this should be as simple as wrapping the update in an array like:
db.findOneAndUpdate({
_id: "Sample"
},[{
$set: {
enabled: {
$not: "$enabled"
}
}
}], {
new: true
}, function(err, result) {})
You can use the $bit operator to toggle the value of the enabled field.
db.findOneAndUpdate({
_id: "Sample"
}, {
{ $bit: { enabled: { xor: 1 } } }
}, {
new: true
}, function(err, result) {})
On each update, the value enabled will toggle (1 to 0, 0 to 1).
Alternatively, you can use the set method as thus:
db.findOneAndUpdate({
_id: "Sample"
}, [{
$set: {
enabled: {
$not: "$enabled"
}
}
}], {
new: true
}, function(err, result) {})

Is it possible to update multiple documents with different values using mongo? [duplicate]

I have the following documents:
[{
"_id":1,
"name":"john",
"position":1
},
{"_id":2,
"name":"bob",
"position":2
},
{"_id":3,
"name":"tom",
"position":3
}]
In the UI a user can change position of items(eg moving Bob to first position, john gets position 2, tom - position 3).
Is there any way to update all positions in all documents at once?
You can not update two documents at once with a MongoDB query. You will always have to do that in two queries. You can of course set a value of a field to the same value, or increment with the same number, but you can not do two distinct updates in MongoDB with the same query.
You can use db.collection.bulkWrite() to perform multiple operations in bulk. It has been available since 3.2.
It is possible to perform operations out of order to increase performance.
From mongodb 4.2 you can do using pipeline in update using $set operator
there are many ways possible now due to many operators in aggregation pipeline though I am providing one of them
exports.updateDisplayOrder = async keyValPairArr => {
try {
let data = await ContestModel.collection.update(
{ _id: { $in: keyValPairArr.map(o => o.id) } },
[{
$set: {
displayOrder: {
$let: {
vars: { obj: { $arrayElemAt: [{ $filter: { input: keyValPairArr, as: "kvpa", cond: { $eq: ["$$kvpa.id", "$_id"] } } }, 0] } },
in:"$$obj.displayOrder"
}
}
}
}],
{ runValidators: true, multi: true }
)
return data;
} catch (error) {
throw error;
}
}
example key val pair is: [{"id":"5e7643d436963c21f14582ee","displayOrder":9}, {"id":"5e7643e736963c21f14582ef","displayOrder":4}]
Since MongoDB 4.2 update can accept aggregation pipeline as second argument, allowing modification of multiple documents based on their data.
See https://docs.mongodb.com/manual/reference/method/db.collection.update/#modify-a-field-using-the-values-of-the-other-fields-in-the-document
Excerpt from documentation:
Modify a Field Using the Values of the Other Fields in the Document
Create a members collection with the following documents:
db.members.insertMany([
{ "_id" : 1, "member" : "abc123", "status" : "A", "points" : 2, "misc1" : "note to self: confirm status", "misc2" : "Need to activate", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") },
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") }
])
Assume that instead of separate misc1 and misc2 fields, you want to gather these into a new comments field. The following update operation uses an aggregation pipeline to:
add the new comments field and set the lastUpdate field.
remove the misc1 and misc2 fields for all documents in the collection.
db.members.update(
{ },
[
{ $set: { status: "Modified", comments: [ "$misc1", "$misc2" ], lastUpdate: "$$NOW" } },
{ $unset: [ "misc1", "misc2" ] }
],
{ multi: true }
)
Suppose after updating your position your array will looks like
const objectToUpdate = [{
"_id":1,
"name":"john",
"position":2
},
{
"_id":2,
"name":"bob",
"position":1
},
{
"_id":3,
"name":"tom",
"position":3
}].map( eachObj => {
return {
updateOne: {
filter: { _id: eachObj._id },
update: { name: eachObj.name, position: eachObj.position }
}
}
})
YourModelName.bulkWrite(objectToUpdate,
{ ordered: false }
).then((result) => {
console.log(result);
}).catch(err=>{
console.log(err.result.result.writeErrors[0].err.op.q);
})
It will update all position with different value.
Note : I have used here ordered : false for better performance.

Mongoose find function to search not returning desired results

I have a list of users in Mongodb that needs to searched according to some filters as shown in below picture:Only gender is mandatory and users may or may not have other details
User.find({
"gender": userEntry.gender,
"dob": { $gte: convert.getDobFromAge(userEntry.ageHigherLimit), $lte: convert.getDobFromAge(userEntry.ageLowerLimit), $exist: false },
"details.chest": { $gte: userEntry.chestLowerLimit, $lte: userEntry.chestHigherLimit, $exist: false },
"details.waist": { $gte: userEntry.waistLowerLimit, $lte: userEntry.waistHigherLimit, $exist: false },
"details.height": { $gte: userEntry.heightLowerLimit, $lte: userEntry.heightHigherLimit, $exist: false },
"details.weight": { $gte: userEntry.weightLowerLimit, $lte: userEntry.weightHigherLimit, $exist: false }
}, function (err, users) {
return res.render('client/search.html', { users: users });
});
Above is the mongoose query to search and the userEntry looks like this
userEntry={
"gender":2,
"ageLowerLimit":28,"ageHigherLimit":40,
"chestLowerLimit":"","chestHigherLimit":"",
"heightLowerLimit":"","heightHigherLimit":"",
"waistLowerLimit":"","waistHigherLimit":"",
"weightLowerLimit":"","weightHigherLimit":"",
"state":"","city":"",
"country":"","skin_color":"",
"profession_type":"","experience":"",
"hair_type":""
}
My problem is the find function , it should search all records with gender as '2' and age>=28 and age=<40 (from the above query gives me empty array even though one record satisfies it),giving all the results that satisfy the above conditions irrespective of whether other fields are empty or doesnot exist.
Any help would be appreciated.
As rightly suggested changed the query to but still 0 records fetched
var query = {
details: {}
};
if (userEntry.gender) {
query.gender = userEntry.gender;
}
if(userEntry.ageLowerLimit && userEntry.ageHigherLimit ) {
query.dbo = { $gte: convert.getDobFromAge(userEntry.ageHigherLimit), $lte: convert.getDobFromAge(userEntry.ageLowerLimit)};
}
console.log(query);
User.find(query, function (err, users) {
if(!err) {
console.log(users);
return res.render('client/search.html', { users: users });
}
console.log(err);
});
});
one of the records trying to fetch
{ "_id" : ObjectId("59c3f47e6388613b94556b78"), "name" : "tanzeel", "email" : "im_tanzeel#yahoo.co.in", "password" : "$2a$10$kvachEZL0vEPPJiS7bIAMeGMXiZ.MRaZmrBECXB207jme1I4JEn6i", "created_at" : ISODate("2017-09-21T17:18:54.822Z"), "role" : 1, "following" : [ ], "dp" : "/dp/default.jpg", "gender" : 2, "__v" : 0, "dob" : ISODate("1994-11-29T00:00:00Z"), "details" : { "height" : 160, "weight" : 65, "profession_type" : "Actor", "skin_color" : "Tan", "eye_color" : "Black", "waist" : 32, "chest" : 35 } }
You got correct value from MongoDB but not your expected value because your query build not correct. Also $exist not valid operator should be $exists and you compare with empty string for some fields like details.waist = "" because userEntry.weightLowerLimit is empty. However you should build query correctly to get expected result.
can try like this...
var query = {
details: {}
};
if (userEntry.gender) {
query.gender = userEntry.gender;
}
if(userEntry.ageLowerLimit && userEntry.ageHigherLimit ) {
query.dbo = { $gte: convert.getDobFromAge(userEntry.ageHigherLimit), $lte: convert.getDobFromAge(userEntry.ageLowerLimit)};
}
if(userEntry.chestLowerLimit && userEntry.chestHigherLimit) {
query['details.chest'] = { $gte: userEntry.chestLowerLimit, $lte: userEntry.chestHigherLimit };
}
//... for others conditions
User.find(query, function (err, users) {
if(!err) {
return res.render('client/search.html', { users: users });
}
console.log(err);
});

Find element in an array that may or may not exist

I have a document that looks a bit like this:
> db.orders.find()
{
_id: ObjectId(),
_reminders: [{
notified: true,
timestamp: ISODate(),
completed: false
}]
}
{
_id: ObjectId(),
_reminders: []
}
What I am trying to find is a document in the orders collection where the "reminders" does not contain a reminder in a specific time range, and is not completed.
db.orders.find({
'_reminders': {
$elemMatch: {
completed: false,
timestamp: { $ne: time }
}
}
});
The problem is that this will not find an order which does not have any reminders at all.
How would one query this?
This should get you what you want
db.getCollection('Clock').find({
$or : [
{
_reminders : {
$elemMatch : {
timestamp : {
$lte : ISODate("2019-07-12T15:35:32.278Z"),
$gte : ISODate("2012-07-12T15:35:32.278Z")
},
completed : false
}
}
},
{
_reminders : {$size : 0}
},
{
_reminders : {$exists : false}
}
]
})
you should use $or query.
db.orders.find({$or: [ { _reminders: [] }, here_put_your_query_with_time_match ]}) - it will return both documents which match your query and these with empty _reminders

MongoDB, remove object from array

Doc:
{
_id: 5150a1199fac0e6910000002,
name: 'some name',
items: [{
id: 23,
name: 'item name 23'
},{
id: 24,
name: 'item name 24'
}]
}
Is there a way to pull a specific object from an array? I.E. how do I pull the entire item object with id 23 from the items array.
I have tried:
db.mycollection.update({'_id': ObjectId("5150a1199fac0e6910000002")}, {$pull: {id: 23}});
However I am pretty sure that I am not using 'pull' correctly. From what I understand pull will pull a field from an array but not an object.
Any ideas how to pull the entire object out of the array.
As a bonus I am trying to do this in mongoose/nodejs, as well not sure if this type of thing is in the mongoose API but I could not find it.
try..
db.mycollection.update(
{ '_id': ObjectId("5150a1199fac0e6910000002") },
{ $pull: { items: { id: 23 } } },
false, // Upsert
true, // Multi
);
I have a document like
I have to delete address from address array
After searching lots on internet I found the solution
Customer.findOneAndUpdate(query, { $pull: {address: addressId} }, (err, data) => {
if (err) {
return res.status(500).json({ error: 'error in deleting address' });
}
res.json(data);
});
my database:
{
"_id" : ObjectId("5806056dce046557874d3ab18"),
"data" : [
{ "id" : 1 },
{ "id" : 2 },
{ "id" : 3 }
]
}
my query:
db.getCollection('play_table').update({},{$pull:{"data":{"id":3}}},{multi:true}
output:
{
"_id" : ObjectId("5806056dce046557874d3ab18"),
"data" : [
{ "id" : 1 },
{ "id" : 2 }
]
}
You can try it also:
db.getCollection('docs').update({ },{'$pull':{ 'items':{'id': 3 }}},{multi:true})
For a single record in array:
db.getCollection('documents').update(
{ },
{'$pull':{ 'items':{'mobile': 1234567890 }}},
{new:true}
);
For a multiple records with same mobile number in array:
db.getCollection('documents').update(
{ },
{
$pull: {
items: { mobile: 1234567890 }
}
},
{ new:true, multi:true }
)
Use $pull to remove the data
return this.mobiledashboardModel
.update({"_id": args.dashboardId}, { $pull: {"viewData": { "_id": widgetId}}})
.exec()
.then(dashboardDoc => {
return {
result: dashboardDoc
}
});
Kishore Diyyana:
If you want to remove all elements including the key of the element attributes list.
Here is the example of mongoDB unset operator:
db.UM_PREAUTH_CASE.update(
{ 'Id' : 123}, { $unset: { dataElements: ""} } )
JSON Look like this:
{ "Id":123,"dataElements" : [ { "createdBy" : "Kishore Babu Diyyana", "createdByUserId" : 2020 }, { "createdBy" : "Diyyana Kishore", "createdByUserId" : 2021 } ] }

Categories

Resources