Adding document to an embedded array after conditional search - arrays

Below is my code returned after the commant db.slist.findOne()
How do I add an id to the array with "iname" as "Soap"?
I need to write a query that seaches the all the "itemlist"array in the collection and if the any document inside an embedded document have "iname" as "Soap", a new row should be inserted above "iname:"Soap" as "itemID" and set an item ID myself. Can someone help me find the correct query for it?
Also this seems like a hard question, please let me know if you are finding hard to understand the question.
{
"_id" : ObjectId("5914213e9f75f9119575c1d7"),
"name" : "Athif",
"age" : 23,
"address" : {
"house" : "675/38B",
"street" : "West Hill",
"city" : "Chungam",
"pincode" : 676507
},
"itemlist" : [
{
"iname" : "Soap",
"quantity" : 2,
"price" : 10,
"rate" : 20
},
{
"iname" : "helmet",
"quantity" : 1,
"price" : 500,
"rate" : 500
},
{
"iname" : "Table",
"quantity" : 2,
"price" : 5000,
"rate" : 10000
}
]
}

For updating array elements mongodb provides positional operator $. when you apply query to find any array elements the positional operator $ holds the position of that embedded document that can be modified in update operation. So, here you go!( suppose your collection name is demo)
db.demo.update({ "itemlist.iname":"Soap" }, { $set:{"itemlist.$.itemID":"your_id"}})

Related

MongoDB - Pull multiple objects from an array

Hi I'm trying to remove multiple objects from an array that looks like this.
{
"_id" : ObjectId("5a7da1bda21d5f3e8cf005b3"),
"owner" : "1",
"group_name" : "PAASCU Board",
"group_members" : [
{
"faculty_name" : "Cheska Dela Rosa",
"faculty_number" : 2,
"_id" : ObjectId("5a7da1bda21d5f3e8cf005b5")
},
{
"faculty_name" : "Earl Sempio",
"faculty_number" : 7323,
"_id" : ObjectId("5a7da1bda21d5f3e8cf005b4")
},
{
"faculty_number" : 203,
"faculty_name" : "Sample",
"_id" : ObjectId("5a7dbf7952bd150a94d83958")
},
{
"faculty_number" : 8025,
"faculty_name" : "Sample Postman",
"_id" : ObjectId("5a7dc64a1cf5dd3d50167d53")
}
],
"__v" : 0 }
It works when I remove a single object using the $pull with this code.
db.getCollection('groups').update({_id: ObjectId("5a7da1bda21d5f3e8cf005b3")}, {$pull: {"group_members": {"faculty_number":8025}}})
But what if I want to remove multiple objects with different faculty_number? I tried using the $each method just like how I add multiple objects in the array but it doesn't work well.
Use $in operator to pass the list of faculty values to remove documents from embedded array. More here
Try
db.groups.update(
{"_id": ObjectId("5a7da1bda21d5f3e8cf005b3")},
{"$pull":{"group_members":{"faculty_number":{$in:[8025,7323]}}}}
)

How to remove a field from an array using aggregate on MongoDB?

{Crs:{[
{Cr: {
"_id" : ObjectId("5a75baada0f20bd4e612d480"),
"Number" : 400,
"Page" : 24,
"DC" : "NE",
}},
{Cr: {
"_id" : ObjectId("5a75baada0f20bd4e612d489"),
"Number" : 300,
"Page" : 14,
"DC" : "100",
}},
]}}
I have this data that i model using aggregate and I would like to know how can i remove "DC" field from all elements of the array using aggregate.
>db.crs.aggregate(
[
{$group : {_id : null, crs : {$push : {cr : "$$ROOT"}}}},
{$project : {_id : 0}}
]
)
This is what did to model that data.
I think its something related with $project.
First of all, your document is not a valid JSON schema. You might reconsider the following schema for that particular document.
{
Crs:[
{
"_id" : ObjectId("5a75baada0f20bd4e612d480"),
"Number" : 400,
"Page" : 24,
"DC" : "NE"
},
{
"_id" : ObjectId("5a75baada0f20bd4e612d489"),
"Number" : 300,
"Page" : 14,
"DC" : "100"
}
]
}
Having above schema, you can easily remove DC field using $unset aggregate operator.
db.crs.aggregate([{$unset: 'Crs.DC'}]);

Create a document with an array from filtered elements in an existing document array

I've asked this question before but not in the clearest way since I had no responses :( so I thought I would try again.
I have a document as shown below, I want to create a new document which only picks the names in the array where language = "English".
{
"_id" : ObjectId("564d35d5150699558156942b"),
"objectCategory" : "Food",
"objectType" : "Fruit",
"objectName" : [
{
"language" : "English",
"name" : "Apple"
},
{
"language" : "French",
"name" : "Pomme"
},
{
"language" : "English",
"name" : "Strawberry"
},
{
"language" : "French",
"name" : "Fraise"
}
]
}
I want the $out document to look like this below. I know I can filter a document by content but, I want to filter within a single document not across a collection. For getting the right document in the first place, I would have a query to $find objectCategory = "Food" and objectType = "Fruit"
{
"_id" : ObjectId("564d35d5150699558156942b"),
"objectCategory" : "Food",
"objectType" : "Fruit",
"objectName" : [
"name" : "Apple",
"name" : "Strawberry"
]
}
Thanks, Matt
wow, ah, I really thought I found it with:
db.serviceCatalogue.find({objectName: {"$elemMatch": {language: "English"}}}, {"objectName.name": 1})
;thanks to: Retrieve only the queried element in an object array in MongoDB collection
However, it did nothing, I must have dreamt it worked. How do you just get the array positions where the value of a field called language = 'English'?
this is only an example of what I want to do, it seems like this is just painful, especially with no-one answering other than me :)

Mongodb Java add new array element in document

I'm using mongo 2.2.3 and the java driver. My dilemma, I have to $push a field and value into an array element, but I cant seem to figure out how to do this. A sample of my data:
"_id" : 1,
"scores" : [
{
"type" : "english",
"score" : 78.97979
},
{
"type" : "spanish",
"score" : 6.99
}
]
I want to push one array attribute ("grade" : "A") in document where type = english.
after push document look like this :
"_id" : 1,
"scores" : [
{
"type" : "english",
"score" : 78.97979,
"grade" : "A"
},
{
"type" : "spanish",
"score" : 6.99
}
]
I tried using shell :
db.Sample.update({"scores.type" : "english"},{"$push" : {"scores": {"grade":"A"}}})
But this is not adding attribute on specific position.
Try this update with Set and reference:
db.Sample.update({"scores.type" : "english"},{"$set" : {"scores.$.grade":"A"}})

How to update an array of multiple elements in mongodb..?

I have a requirement to update an array of multiple elements.My collections is in the following way
{
"_id" : ObjectId("53e87e239ae974e6a0a81004"),
"name" : "mulagala",
"notifications" : [
{
"name" : "apple",
"status" : 0
},
{
"name" : "microsoft",
"status" : 0
},
{
"name" : "android",
"status" : 0
}
]
}
now i want to change the every status element of the array should be changed to 1, ie.status:1 with a single query.
I tried in the following way
db.mystatus.update({'notifications.status':0},{$set:{'notifications.$.status':1}},false,true)
But the first record only updating, what to do.Any help would be appriciated!
Have you tried updating the elements of the array using the $ operator for arrays? Currently it updates only one element as the index is coded to 0.

Resources