I have data in mongoDB as follows
_id : 5d91caf461f93f13e48ac307,
restaurants : [
{
name : 'grace restaurant',
menus : [
{
menu_name : 'chicken soup',
price : 100
},
{
menu_name : 'Biriyani',
price : 250
},
]
},
{
name : 'river side restaurant',
menus : [
{
menu_name : 'veg lollipop',
price : 47
},
{
menu_name : 'Grill chicken',
price : 210
},
]
}
]
Now, I need to update the river side restaurant's menu "Grill chicken's" price to 310.
How can I do that with mongoose or mongoDB function,I have used lot of different functions but no use, Please help me to find this.
Thank you
We have to use the array filters for the same.
The following is an example:
db.collection.update({},
{
$set:{
"restaurants.$[restaurantFilter].menus.$[menuFilter].price": 310
}
},
{
"arrayFilters":[
{
"restaurantFilter.name":"river side restaurant"
},
{
"menuFilter.menu_name":"Grill chicken"
}
]
})
Before update:
{
"_id" : "5d91caf461f93f13e48ac307",
"restaurants" : [
{
"name" : "grace restaurant",
"menus" : [
{
"menu_name" : "chicken soup",
"price" : 100
},
{
"menu_name" : "Biriyani",
"price" : 250
}
]
},
{
"name" : "river side restaurant",
"menus" : [
{
"menu_name" : "veg lollipop",
"price" : 47
},
{
"menu_name" : "Grill chicken",
"price" : 210
}
]
}
]
}
After update:
{
"_id" : "5d91caf461f93f13e48ac307",
"restaurants" : [
{
"name" : "grace restaurant",
"menus" : [
{
"menu_name" : "chicken soup",
"price" : 100
},
{
"menu_name" : "Biriyani",
"price" : 250
}
]
},
{
"name" : "river side restaurant",
"menus" : [
{
"menu_name" : "veg lollipop",
"price" : 47
},
{
"menu_name" : "Grill chicken",
"price" : 310
}
]
}
]
}
Related
does anyone know how to change a propertie value in mongo
from
i changed JSON.units[0].services[0].label from etiket controle to AuditLabel
i tried this:
db.getCollection('assortiment').updateMany({"units.$[].services.$[].label": "etiket controle"},{ "$set": { "units.$[].services.$[].label": "AuditLabel" }})
but no succes because it dont select anything
{
"_id" : "764",
"meta" : {
"groupsId" : "764",
"type" : "DRYGR"
},
"units" : [
{
"unit" : "BASE_UNIT_OR_EACH",
"gtin" : "08711728556206",
"services" : [
{
"label" : "etiket controle",
"collection" : "gtins"
}
]
}
]
}
to
{
"_id" : "764",
"meta" : {
"groupsId" : "764",
"type" : "DRYGR"
},
"units" : [
{
"unit" : "BASE_UNIT_OR_EACH",
"gtin" : "08711728556206",
"services" : [
{
"label" : "AuditLabel",
"collection" : "gtins"
}
]
}
]
}
Try this code its work's for me
db.getCollection('assortiment').updateMany(
{
"units.services.label": "etiket controle"
},
{
"$set":
{ "units.$[].services.$[].label": "AuditLabel" }
}
)
I have user_data field in my elasticsearch index document for eg;
sample doc:
user_data : [
{
userid :1,
order_id :32
},
{
userid :8,
order_id : 99
}
]
I have another set of user data in my memory which i want to extend (adding list to list)
data in memory:
preserved_user_data : [
{
userid :991,
order_id :89
},
{
userid :89,
order_id :76
}
]
I want to update that document to :
user_data : [
{
userid :1,
order_id :32
},
{
userid :8,
order_id : 99
},
{
userid :991,
order_id :89
},
{
userid :89,
order_id :76
}
]
i have tried using script ctx._source.user_data += params.preserved_user_data but it makes the document's field empty array.
P.S : I am doing this via parallel bulk using op type : update on ES 7.1.1
A sample query using update script to append array to array in document
Query:
POST index35/_update/tg8DwHAB4HO3_VSZUdzR
{
"script": {
"lang": "painless",
"inline": "if(!ctx._source.containsKey('user_data')){ctx._source['user_data']=[]} for(int i=0;i<params.data.length;i++){ctx._source.user_data.add(params.data[i])}",
"params": {
"data": [
{
"userid": 991,
"order_id": 89
},
{
"userid": 89,
"order_id": 76
}
]
}
}
}
Result:
"hits" : [
{
"_index" : "index35",
"_type" : "_doc",
"_id" : "tg8DwHAB4HO3_VSZUdzR",
"_score" : 1.0,
"_source" : {
"user_data" : [
{
"userid" : 1,
"order_id" : 32
},
{
"userid" : 8,
"order_id" : 99
},
{
"userid" : 991,
"order_id" : 89
},
{
"userid" : 89,
"order_id" : 76
}
]
}
}
]
}
I am trying to update the store = 465 , AisleName = 59 and set AisleSort = 34 by this update query for the below code
db.getCollection('products').update({'AvailableOnStores.AisleName': { '$eq': '59' }, 'AvailableOnStores.StoreNumber': { '$eq': '465' } }, { '$set': { 'AvailableOnStores.$.AisleSort': 34 } } )
Then it is updating with 34 in the 465 store but we don't have that AisleName in that store. we have that aisle name in store 423. I cannot check element by element, as I am checking all these from json file that contains sort and AisleName for each store.
{
"ProductCode" : "6786777",
"AvailableOnStores" : [
{
"StoreNumber" : "465",
"Price" : "19",
"AisleSort" : 9,
"AisleName" : "Checkout Lane",
"AisleLocations" : [
{
"bayNumber" : 6,
"description" : "Checkout Lane",
}
]
},
{
"StoreNumber" : "423",
"Price" : "1",
"AisleSort" : 5,
"AisleName" : "59",
"AisleLocations" : [
{
"description" : " Aisle 59",
},
{
"description" : "Aisle 25",
},
{
"description" : "Aisle 4",
}
]
}
],
"NotAvailableOnStores" : [],
"IsPricingVaries" : false
}
If you want to update the document in which both of these conditions gets true you should modify your query like this
db.getCollection('products').update(
{
'AvailableOnStores':{
$elemMatch:{'AisleName':{ '$eq': '59' },'StoreNumber':{ '$eq': '465' }}}
},
{
'$set': {'AvailableOnStores.$.AisleSort': 34 }
}
)
For reference read this documentation on mongodb $elemMatch and let me know if you need more help
There are multiple collections one of these are.
users
place
The document stored in the database which is given below.
{
"_id" : "1eca51f0-538d-11e8-89fc-d125355b590d",
"organization" : [
{
"associationIds" : {
"dObjectIds" : [
{
"dObjectData" : {
"objectId" : "fd047ddf-61c2-4df2-8241-62af0399c8c1",
"objectName" : "shift",
"dObjectId" : "49492544-e4e3-491b-b967-a6f62330da3f"
}
}
]
},
"organizationId" : "1c3f6150-538d-11e8-89fc-d125355b590d",
"privelege" : "Admin",
"organizationName" : "ABC"
},
{
"associationIds" : {
"dObjectIds" : [
{
"dObjectData" : {
"objectId" : "ad047ddf-61c2-4df2-8241-62af0399c5c1",
"objectName" : "shift",
"dObjectId" : "38492544-e4e3-491b-b967-a6f62330da3f"
}
},
{
"dObjectData" : {
"objectId" : "fd047ddf-61c2-4df2-8241-62af0399c8c2",
"objectName" : "department",
"dObjectId" : "49492544-e4e3-491b-b967-a6f62330da1f"
}
}
]
},
"organizationId" : "1c3f6150-538d-11e8-89fc-d125355b591d",
"privelege" : "Manager",
"organizationName" : "XYZ"
}
]}
I'm using these query.
db.users.aggregate([
{$match: {'_id': '1eca51f0-538d-11e8-89fc-d125355b590d'}},
{$unwind :'$organization'},
{$match:{'organization.organizationId':'1c3f6150-538d-11e8-89fc-d125355b591d'}},
{$unwind:'$organization.associationIds.dObjectIds'},
{'$lookup': {
'from': 'places',
localField:'organization.associationIds.dObjectIds.dObjectData.dObjectId',
foreignField: 'dObjectData.dObjectId',
as: 'associationData'
}
},
{'$project': {
'associationData': 1
}
}
])
I have used $unwind two times in a query which has more expensive.Is there any way or operation which can we use instead of multiple times of $unwind.
NOTE: I waana result like this(result of associationData which get the velue from other collection so I used $lookup)
{
_id" : "c6114ee0-c82f-11e6-9b1e-6140212f8693",
"associationData" : [
{
"_id" : "f09f5d70-ad7e-11e7-87bd-cd873956707e",
"dObjectData" : {
"departmentName" : "CEO & Directors",
"dObjectId" : "ab034106-ad7d-11e7-abc4-cec278b6b50a",
"objectName" : "Departments",
"objectId" : "63735ee2-ad79-11e7-abc4-cec278b6b50a",
"updatedOn" : ISODate("2017-10-10T11:20:37.255+05:30"),
"addedOn" : ISODate("2017-10-10T11:20:37.255+05:30")
},
"associationIds" : {
"organizationId" : "af39bc69-1938-4149-b9f7-f101fd9baf73",
"placeIds" : [ ],
"assignmentIds" : [ ],
"beaconIds" : [ ],
"userIds" : [ ]
}
}]}
I'll be very thankful.
monogodb databse:
{ "title" : "A vs B ha",
"status" : 1,
"items" : [
{ "name" : "0",
"num" : 0,
"men" : [
{ "user" : "id1",
"score" : 300 },
{ "user" : "id2",
"score" : 1300 },
{ "user" : "id3",
"score" : 400 } ] },
{ "name" : "1",
"num" : 0,
"men" : [
{ "user" : "id11",
"score" : 3300 },
{ "user" : "id23",
"score" : 13400 },
{ "user" : "id3",
"score" : 4000 } ] },
{ "name" : "2",
"num" : 0,
"men" : [] } ],
"_id" : ObjectId( "538db59fa332f765c0f2c210" ) }
This Node.js code doesn't work:
collection.findAndModify(
{"_id": idSearch
,"items.name":sBetItem,"items.men.user":sUser}
,[]
, {
$set:{
"items.men.user":sUser}
,$inc: { "items.men.score": sScore }
}
, {new:true, upsert:true}
,function(err, docUser) {
if (err){
console.warn(err.message); // returns error if no matching object found
}else{
console.dir(docUser);
}
});
I want to search a player's score, if it doesn't exist, insert new;
if it already exist, increase his score number.
But it has error:exception: E11000 duplicate key error index