I am having below format document
{
"_id":"5f10481e74d83d4b726fdf33",
"name":"1234",
"settings":{
"allowedUser":true,
"theme":"lowergrade"
}
}
I want to write update query for settings.theme, so I have written below mongo query
db.test.update({
_id: "5f10481e74d83d4b726fdf33"
},
{
$set: {
"settings": {
"theme": "lowergrade123",
}
}
})
Update is happening proper but "allowedUser" : true, key and value was deleted.
This is my expected answer
{
"_id":"5f10481e74d83d4b726fdf33",
"name":"1234",
"settings":{
"allowedUser":true,
"theme":"lowergrade123"
}
}
db.test.update({ _id: "5f10481e74d83d4b726fdf33" }, {
$set: {
"settings.theme": "lowergrade123",
}
})
You need to use . to set a value of a field. You are actually replacing settings.
Related
I have a collection like this one:
{
"citizen":[
{
"country":"ITA",
"language":"Italian"
},
{
"country":"UK",
"language":"English"
},
{
"country":"CANADA",
"language":"French"
}]
}
I'm trying to update the collection but with a certain condition. I need to update remove an element of the array citizen if the value in the field country is longer than 3 characters.
I got that to remove an element i have to use $pull, to check the size of a string I have to use $strLenCP and $gt is greater than, but I'm struggling to put them together.
The result of the update should be:
{
"citizen":[
{
"country":"ITA",
"language":"Italian"
},
{
"country":"UK",
"language":"English"
}]
}
Any suggestions?
EDIT:
with the collection, as it is, the command:
db.getCollection('COLLECTION').update( { }, { $pull: {"citizen": {"country": /^[\s\S]{4,}$/}}}, { multi: true })
works perfectly.
I tried it on another collection as this one:
{
"cittadino":{
"citizen":[
{
"country":"ITA",
"language":"Italian"
},
{
"country":"UK",
"language":"English"
},
{
"country":"CANADA",
"language":"French"
}]
}
}
and it doesn't update anymore. What should i do?
You're on the right path:
db.getCollection('COLLECTION').update( { }, { $pull: {"citizen": {"country": /^[\s\S]{4,}$/}}}, { multi: true })
$pull operator takes a value or a condition. You need to use this to filter the array based on the "country" property
EDIT:
Use the dot-notation to access the nested document
db.getCollection('COLLECTION').update( { }, { $pull: {"cittadino.citizen": {"country": /^[\s\S]{4,}$/}}}, { multi: true })
I have a collection tblTesting with records saved like
{
"_id": ObjectId("5de9f044af647f21780056e1"),
"name": "abc",
"creditAccountDetails": {
"creditAccountNumber": "0200040671890190",
"creditAccountNumberId": "5db2efb5590a065abc006b12"
}
}
The embedded document "creditAccountDetails" has been wrongly saved. Now I am trying to update them by using mongodb command like
db.tblTesting.updateMany
{},
[
{ $set: { creditAccountDetails: [[ 'creditAccountNumberId' : ObjectId ($creditAccountNumber) ]] } },
{}
]
)
Basically I want that the command should be able to update all the records like
{
"_id": ObjectId("5de9f044af647f21780056e1"),
"name": "abc",
"creditAccountDetails":[ {
"creditAccountNumber": "0200040671890190",
"creditAccountNumberId": ObjectId("5db2efb5590a065abc006b12")
}
]
}
Please help!!!
Note that I am using mongo db 4.0
Since you are on mongodb version 4.0, which does not allow referring document fields in an update. A way to do this is iterate via cursor on the collection and update the field.
var cursor = db.collection.find({});
while (cursor.hasNext()) {
var doc = cursor.next();
db.collection.updateOne(
{
_id: doc._id
},
{
$set: {
creditAccountDetails: [
{
creditAccountNumber: doc.creditAccountDetails.creditAccountNumber,
creditAccountNumberId: ObjectId(
doc.creditAccountDetails.creditAccountNumberId
)
}
]
}
}
);
}
For readers who are on Mongodb 4.2.0+ which allows using aggregation pipeline ops in update methods updateOne, updateMany where document fields can be used as part of $set.
db.collection.updateMany({}, [
{
$set: {
creditAccountDetails: [
{
creditAccountNumber: "$creditAccountDetails.creditAccountNumber",
creditAccountNumberId: {
$toObjectId: "$creditAccountDetails.creditAccountNumberId"
}
}
]
}
}
]);
tblTesting's schema should have define type ObjectId for creditAccountNumberId
below example I used mongoose
const tblTesting = mongoose.Schema({
....
creditAccountNumberId: {
type: mongoose.Schema.Types.ObjectId,
},
....
}, {
collection: 'tblTesting',
timestamps: true,
strict: false,
})
I am trying to remove an entry in an array that is a sub property of a document field.
The data for a document looks like this:
{
_id: 'user1',
feature: {
enabled: true,
history: [
{
_id: 'abc123'
...
}
]
},
...
}
For some reason I have not been able to remove the element using $pull and I'm not sure what is wrong.
I've looked at the official docs for $pull, this well-known answer, as well this one and another.
I have tried the following query
db.getCollection('userData').update({ _id:'user1' }, {
$pull: {
'feature.history': { _id: 'abc123' }
}
})
and it has no effect. I've double-checked _id and it is a proper match. I've also tried filtering based on the same entry, thinking I need to target the data I'm trying to remove:
db.getCollection('userData')
.update({ _id: 'user1', 'feature.history': { _id: 'abc123' }, { ... })
So far no luck
You need to cast your id to mongoose ObjectId
db.getCollection('userData').update(
{ "_id": "user1" },
{ "$pull": { "feature.history": { "_id": mongoose.Types.ObjectId(your_id) } }
})
db.getCollection('userData').update({ _id:'user1', "feature.history._id" : "abc123" }, {
$pull: {
'feature.history.$._id': 'abc123'
}
})
I have the following query:
db.getCollection('MyCollection').find({
$or: [{
"Zips": {
$elemMatch: { "ZipCode5": "95757" , "ZipCode4": "6237"}
}
}, {
"Zips": {
$elemMatch: { "ZipCode5": "95757" , "ZipCode4": "0000"}
}
}]
})
I have both documents on my collection, but I want only to return the document that matches the first criteria if both exist, and the 2nd if the first dosn't exist.
Currently, the above query returns both if they both exist.
Why not just limit to first row then?..
db.getCollection('MyCollection').find({
$or: [{
"Zips": {
$elemMatch: { "ZipCode5": "95757" , "ZipCode4": "6237"}
}
}, {
"Zips": {
$elemMatch: { "ZipCode5": "95757" , "ZipCode4": "0000"}
}
}]
}).limit(1)
I'm trying to mass update some mongo documents.
I'm using the query
db.articles.update(
{
'categories.id': ObjectId("51cd5272222wb6zs464fa4d9"),
'source.importer': 'pa'
},
{
$set :
{
'source.expires-at': ISODate("2014-01-01T08:39:45Z")
}
}
)
This query does not update the source.expires-at field, however the where part of the statement works fine.
The document structure is
{
"_id": ObjectId("5211dc100000044707000015"),
"categories": {
"0": {
"id": ObjectId("51cd5272222wb6zs464fa4d9")
}
},
"source": {
"importer": "pa",
"expires-at": ISODate("2013-09-18T08:49:32.0Z")
}
}
Try this:
db.articles.update(
{
'categories.id': ObjectId("51cd5272222wb6zs464fa4d9"),
'source.importer': 'pa'
},
{
$set: {
'source.expires-at': ISODate("2014-01-01T08:39:45Z")
}
},
{ multi: true }
)
You will need to pass additional option argument {multi: true};
Look in to this https://education.10gen.com/courses/10gen/M101JS/2013_August/courseware/CRUD/Multi-update/