So i have a collection called 'users'. Where it lists a list of users and profile info.They are all stored inside mongodb. I want to add the option where they can add more pictures to their profile.For example I just want to add an extra "4":"newpicture.png" to "Documents" in the item inside the collection. How do I add a subsection to this item without having to rewrite all the data?
Eample of current data
{
"name": "Oran",
"username": "Oran.Hammes",
"avatar": ".jpg",
"email": "[email protected]",
"dob": "1953-03-21T17:40:17.020Z",
"phone": "364-846-1607",
"address": {
"street": "Schultz Stream",
"suite": "Suite 618",
"city": "North Muriel mouth",
"zipcode": "06447-1081",
"geo": {
"lat": "57.1844",
"lng": "-56.8890"
}
},
"website": "misty",
"pany": {
"name": "Hettinger, Reilly and Stracke",
"catchPhrase": "Multi-tiered system-worthy database",
"bs": "best-of-breed evolve e-markets"
},
"Documents":{
"1":"image.png",
"2":"Test.jpg",
"3":"Next.png"
}
}
with .update({ username: "Oran.Hammes" },{$set: {"Documents" :{"4": "newpicture.png"}}})
So i have a collection called 'users'. Where it lists a list of users and profile info.They are all stored inside mongodb. I want to add the option where they can add more pictures to their profile.For example I just want to add an extra "4":"newpicture.png" to "Documents" in the item inside the collection. How do I add a subsection to this item without having to rewrite all the data?
Eample of current data
{
"name": "Oran",
"username": "Oran.Hammes",
"avatar": "https://s3.amazonaws./uifaces/faces/twitter/brandonflatsoda/128.jpg",
"email": "[email protected]",
"dob": "1953-03-21T17:40:17.020Z",
"phone": "364-846-1607",
"address": {
"street": "Schultz Stream",
"suite": "Suite 618",
"city": "North Muriel mouth",
"zipcode": "06447-1081",
"geo": {
"lat": "57.1844",
"lng": "-56.8890"
}
},
"website": "misty",
"pany": {
"name": "Hettinger, Reilly and Stracke",
"catchPhrase": "Multi-tiered system-worthy database",
"bs": "best-of-breed evolve e-markets"
},
"Documents":{
"1":"image.png",
"2":"Test.jpg",
"3":"Next.png"
}
}
with .update({ username: "Oran.Hammes" },{$set: {"Documents" :{"4": "newpicture.png"}}})
Share Improve this question edited Sep 22, 2017 at 17:57 CommunityBot 11 silver badge asked Jan 1, 2016 at 2:40 AJ_AJ_ 3,98711 gold badges50 silver badges84 bronze badges2 Answers
Reset to default 3Mongo has the $set
part of its update mand for updating values within a single document. You can read more about modifying documents here: https://docs.mongodb/manual/tutorial/modify-documents/
An example would be:
db.users.update(
{ "username": "Oran.Hammes" },
{
$set: {
"Documents.4" : "newpicture.png"
}
}
)
With regards to whether or not you will need to rewrite the data, refer to this question: Does MongoDB $set write just the field or the whole document? . Basically, this depends on how you setup your database, and how much memory is allocated to each document.
you can use db.collection.update:
db.users.update(
{ "username": "Oran.Hammes" },
$set: { "Documents.4": "newimage.jpg" } });
the first curly is filter to get reference your record(called document in MongoDB), and then you only add new item(s) to your record. Hope this helps.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745227446a4617527.html
评论列表(0条)