Reputation: 407
I have the following schema
var myschema = new schema({
username: { type: String, required: validationMessages.required, index: { unique: true }, lowercase: true },
siteConfig:{ type: Array}
});
And my document will be look like this
{
"_id" : ObjectId("5411a6fb4bcc787927825dac"),
"username" : "admin"
"siteConfig" : [
{
"name" : "landing"
"page" : {
"name" : "home",
"sections" : [
{
"name" : "header",
},
{
"name" : "bodycontent",
},
{
"name" : "footer",
}
]
}
}
}
I want to update the property “name”:”header” under siteConfig->page->sections WHERE the criteria is match the following
(“username”:”admin” && “siteConfig.name”: “landing “&& “page.name”:”home”)
I have trired with dot notation and elematch but I don't know the right way to write update query.
Upvotes: 0
Views: 213
Reputation: 4238
I think the problem might be that you haven't defined the schema of siteConfig
. First, update your schema like this:
var ObjSchema = new Schema({
...
siteConfig: [
{
name: {type: String},
page: {
name: {type: String},
sections: [
{
name: {type: String},
}
],
},
}
],
});
Now you can update like this:
var query = {
username: 'admin',
'siteConfig.name': 'landing',
'siteConfig.page.name': 'home',
};
var update = {
$set: {
'siteConfig.page.sections.0.name': 'header',
},
};
var options = {
multi: true,
};
Obj.update(query, update, options, function(err, numAffected) {
...
});
I wasn't sure what you wanted to update so you can change the update
dict how you want. Change the options
dict if you don't want to update multiple documents.
Upvotes: 1