This Meteor server code tries to update Mongodb collection but gives error:
let originalDoc = original.fetch()[0];
Meteor.users.update(userId, {
$set: {
profile: originalDoc.profile,
cmpProfile: originalDoc.cmpProfile,
aaa: originalDoc.aaa
},
$unset: {
'profile.abc': 1
}
});
The error:
Exception while invoking method 'xyz' MongoError: Cannot update 'profile' and 'profile.abc' at the same time
Any ideas? thx
The error is pretty self-explanatory: you can't at the same time $set whole profile and $unset profile.abc because MongoDB does not allow such operations.
Instead of calling $unset you could do delete originalDoc.profile.abc; before running query and that will effectively remove abc field from profile as you're setting whole embedded document.
Related
I have a string field which consists of date, I am trying to run following command mentioned in Update string to Date object in mongodb
db.getCollection('rft').updateMany(
{},[{ "$set": { "createdate": { "$toDate": "$create_date" } }}]
);
Getting an error:
Error: Failed to execute script.
Error: the update operation document must contain atomic operators
Details:
DBCollection.prototype.updateMany#src/mongo/shell/crud_api.js:625:1
#(shell):1:1
Can someone please help in updating the records to get new field with date time.
Your query seems to work as expected , check playgroud unless something different is expected ...
I want to rename a field in MongoDB, then setting new Value with the old field Name: here is the example :
db.exemption.update({'ref':163},{$rename:
{'request.reference':'request.oldRef'},$set:
{'request.reference':'00000'}},true,true)
but heri the error that I got
{ [MongoError: Cannot update 'request.reference' and 'request.reference' at the same time]
name: 'MongoError',
code: 16837,
err: 'Cannot update \'request.reference\' and \'request.reference\' at the same time' }
Is there a possible way to do the update of this field inspite of using 2 queries mongo?
You could do
db.exemptions.update({ref: <value>}, {
$unset: {'request.oldRef': 1},
$set: {'request.reference': <new value>}
});
It doesn't really rename the field, but it gives you the same result.
I am trying to add a new field into all documents in an existing collection.
Database name = test
Collection name = teams
test.teams.update({
}
,
{
$set: {
"isGolden": false
}
}
,
false,
true)
When I am trying it with MongoChef, it is giving me the following error:
What is wrong with this?
Thanks
If you want to update all the documents, use something like this:
db.teams.update({}, {$set: {isGolden: false}}, {multi: true});
your are selecting all, setting the field isGolden to false and making this update in all documents using multi: true
With MongoDB 3.2 or higher version, you can do
db.teams.updateMany({}, {$set: {isGolden: false}});
Here's the doc for updateMany function db.collection.updateMany()
With Studio 3T you were writing queries in JSON mode in which it wants JSON data but you are not writing JSON Query. You must go to InteliShell mode in which your query will execute in format that you were writing.
I'm using mongodb shell and trying to do a collection query within another collection query. Is it valid to do this:
db.watchers.find( { login: { "$nin": [db.results.find()] } } )
I'm trying to see if the login id from db.results.find() is in db.watchers.find(). Can mongodb even do that?
Mongodb does not let you do subqueries. This is invalid.
Hi im trying to simply remove a document from a collection using mongoose but for some strange reason I cannot get it to work.
Here is the code:
function deleteUserevent()
{console.log('in delete User Event');
models.Userevent.remove({ _id: "5214f4050acb53fe31000004"}, function(err) {
if (!err){
console.log('deleted user event!');
}
else {
console.log('error');
}
});
}
Can anyone help me out on my syntax? I know the _id is stored as new ObjectId("5214f4050acb53fe31000004") but I have tried this with no joy?
Thanks.
In MongoDB, the "_id" field of documents is of type ObjectId, as you mentioned. This is not equal to a String, so running the query
db.userevent.remove({ _id: "5214f4050acb53fe31000004"});
will not match anything, and will not remove anything. Instead, you must search for a document where the _id field is an ObjectId with that value:
db.userevents.remove({ _id: ObjectId("5214f4050acb53fe31000004")});
In mongoose, you can use the findByIdAndRemove command to remove a document with a specific _id. This command takes either an ObjectId or a String as an argument, so
query = Userevent.findByIdAndRemove("5214f4050acb53fe31000004");
should work just fine.
Just add exec() after query.
It should work like this:
await models.Userevent.findByIdAndDelete("5214f4050acb53fe31000004").exec()