{
"_id" : ObjectId("5b8d1ecbb745685c31ad8603"),
"name" : "abc",
"email" : "abc#gmail.com",
"projectDetails" : [
{
"technologies" : [
"abc",
"abc"
],
"_id" : ObjectId("5b8d1ecbb745685c31ad8604"),
"projectName" : "abc",
"projectDescription" : "abc",
"manager" : "abc",
"mentor" : "abc"
}
],
"__v" : 0
}
Here, projectDetails is an array of objects. I want to update the element "projectName" in projectDetails. How do I write a PUT request for the same in Postman?
Try below query
db.users.update({ "email" : "abc#gmail.com","projectDetails._id":ObjectId("5b8d1ecbb745685c31ad8604")},{ $set: { "projectDetails.$.projectName" : "test" } })
Your url should be like this
http://localhost:3000/project/5b8d1ecbb745685c31ad8604
Your put request as mentioned below
router.route("/updateProject",function(req,res){
var id = req.query.project_id; // Check syntax for framework you are using
});
Related
Here is my organization collection.
[{
"_id" : ObjectId("5fd5fc1b9f117029b5233b2e"),
"name" : "ClassA",
"orgMembers" : [
{
"_id" : ObjectId("5fd5fc1b9f117029b5233b2f"),
"userId" : "Ben",
},
{
"_id" : ObjectId("5fd5fc1b9f117029b5233b2f"),
"userId" : "Anton",
}
],
},
{
"_id" : ObjectId("5fd5fc1b9f117029b5233b2e"),
"name" : "ClassA",
"orgMembers" : [
{
"_id" : ObjectId("5fd5fc1b9f117029b5233b2f"),
"userId" : "Ben",
}
],
}]
Each document has properties like _id, name, orgMembers which represent document information.
And orgMembers is the Array of Members (_id, userId) who belongs to organization.
In this collection, I want to fetch the organizations which includes orgMember with Anton as userId and as well orgMembers of fetched organization document should only contain Anton as a orgMember.
Expected Result is likewise
[{
"_id" : ObjectId("5fd5fc1b9f117029b5233b2e"),
"name" : "ClassA",
"orgMembers" : [
{
"_id" : ObjectId("5fd5fc1b9f117029b5233b2f"),
"userId" : "Anton",
}
],
}]
Here ClassA organization has two orgMembers but need to be filtered matching with userId.
I have tried with
documentModel.find({ 'orgMembers.userId': 'Anton' })
But within this query, I get the result like this.
[{
"_id" : ObjectId("5fd5fc1b9f117029b5233b2e"),
"name" : "ClassA",
"orgMembers" : [
// should be omitted
{
"_id" : ObjectId("5fd5fc1b9f117029b5233b2f"),
"userId" : "Ben",
},
// should be contained
{
"_id" : ObjectId("5fd5fc1b9f117029b5233b2f"),
"userId" : "Anton",
}
],
}]
For expected result, orgMember with userId: Ben should be omitted.
How can I get my expected result with mongo query?
I believe this will be worked on your side
db.collection.find({
"orgMembers.userId": "Anton"
},
{
orgMembers: {
"$elemMatch": {
userId: "Anton"
}
}
})
not sure if i quite got your requirement:
but try this if it works
db.collection.find({
"orgMembers.userId": {
$regex: "Anton",
$options: "i"
}
},
{
name: 1,
"orgMembers.$": 1
})
this is to return only the userId you are looking for in orgMembers.if there are more orgmembers they will not be part of output
This is the structure of the file.
{
"_id" : ObjectId("5f45274bbbe768fe0d56d9d0"),
"article" : "article-name",
"comments" : [
{
"id" : "_lj7cfhx7z",
"name" : "person",
"date" : "25/08/2020",
"text" : "asdasdsddsdsdasd",
"replies" : [
{
"name" : "person2",
"date" : "25/08/2020",
"text" : "replyyyteextttttt"
},
{
"name" : "person3",
"date" : "25/08/2020",
"text" : "replyyyteextttttt"
}
]
},
{
"id" : "_wpdpj7kv6",
"name" : "person4",
"date" : "25/08/2020",
"text" : "aaaa",
"replies" : [ ]
}
]
}
I am trying to write a function that appends to replies list given the id of the comment. When I do it in the MongoDB shell like this db.comments.findOneAndUpdate({"article": "exif-parsing", "comments.id": "_t2aqbhi5a"}, {"$push": {"comments.$.replies": {"text": "new"}}});, it works. But in Node.js it doesn't work.
var db = new DataBase("mongodb://localhost:27017/")
replyTo(article, id, name, comment) {
db.connect("blog", "comments", function(collection) {
var reply = {name: name, date: getCurrentDate(), text: comment};
collection.findOneAndUpdate({"article": article, "comments.id": id}, {"$push": {"comments.$.replies": reply}});
});
}
I want to update Itinirary which is embedded subdocument inside the
packages which itself is a embedded document.I tried using index and
was successful like this :
"$set":{'packages.$.itinerary.0.to': "kausaltar"}
but I don't want to use index like 0,1 in itinerary update.Please can you help me.
My JSON schema is like this :
{
"_id" : ObjectId("5e1ca76b9f96d17c449de177"),
"org_id" : "22222222222",
"packages" : [
{
"_id" : ObjectId("5e1ca76b9f96d17c449de17a"),
"region" : "ppopopop",
"itinerary" : [
{
"_id" : ObjectId("5e1ca76b9f96d17c449de17d"),
"id" : 1,
"from" : "ppopopop",
"to" : "ashinnkn",
"mode" : "4444444444",
"day" : 2,
"duration_hrs" : 3
},
{
"_id" : ObjectId("5e1ca76b9f96d17c449de17c"),
"id" : 2,
"from" : "44444444444",
"to" : "Faketon2",
"mode" : "4444444444",
"day" : 2,
"duration_hrs" : 3
},
{
"_id" : ObjectId("5e1ca76b9f96d17c449de17b"),
"id" : 3,
"from" : "55555555555",
"to" : "ashin",
"mode" : "55555555",
"day" : 2,
"duration_hrs" : 3
}
],
"image_url" : "sadfasfsa",
},
{
"_id" : ObjectId("5e1ca76b9f96d17c449de178"),
"region" : "bktll",
"itinerary" : [
{
"_id" : ObjectId("5e1ca76b9f96d17c449de179"),
"id" : 1,
"from" : "mmkkkkm",
"to" : "ashin",
"mode" : "SkkkkA",
"day" : 2,
"duration_hrs" : 3
}
],
"image_url" : "sadfasfsa",
}
]
}
}
I want to update itinerary field inside packages which itself is an embedded document.
And my code is like this :
AgentPackage.update({
"_id" : mongoose.Types.ObjectId("5e1ca76b9f96d17c449de177"),
"packages.region" : "ppopopop",
'packages.itinerary.id': 2,
}, {$set: {'packages.itinerary.$$.from': "test" }}
I am not being able to update.I don't want to use indexing like 0,1 INSIDE query.
Try this :
If you've only one object inside packages with region : "ppopopop", then try this (this should suffice in most cases as you might not have duplicate regions with same name) :
AgentPackage.update({
_id: ObjectId("5e1ca76b9f96d17c449de177"), "packages.region": "ppopopop"
}, { $set: { "packages.$.itinerary.$[item].to": 'kausaltar' } }, {
arrayFilters: [{ 'item.id': 2 }]
})
If you've got multiple objects inside packages with region : "ppopopop", then try this :
AgentPackage.update({
_id: ObjectId("5e1ca76b9f96d17c449de177"), "packages.region": "ppopopop" // Here this "packages.region": "ppopopop" is optional as _id will only bring one document else might be helpful to bring only docs with region: ppopopop right after filter.
}, { $set: { "packages.$[eachPackage].itinerary.$[eachItinerary].to": 'kausaltar' } }, {
arrayFilters: [{ 'eachPackage.region': "ppopopop" }, { 'eachItinerary.id': 2 }]
})
Ref : update-positional-filtered
You can do it with $arrayfilter
db.getCollection("unit").update({_id: ObjectId("5e1ca76b9f96d17c449de177")}, { $set: { "packages.$[t].itinerary.$[d].from": "test" } },
{ arrayFilters: [ { "t.region": "ppopopop" }, {"d.id":15}]})
i'm trying - find query for array in mongodb,
db.users.find({posts:{$all:[_id : ObjectId("5cc83a158ceb346aa58b1161")]}})
db.users.find( { "posts.post" : {$all : []}})
//My MongoDB collections
> db.users.find().pretty()
{
"_id" : ObjectId("5cc83a158ceb346aa58b1161"),
"friends" : [ ],
"name" : "krishna soni",
"email" : "krishnasoni#gmail.com",
"password" : "krishna",
"posts" : [
{
"_id" : ObjectId("5cc8478118195b71e2ccd517"),
"post" : "hey users",
"createdTime" : ISODate("2019-04-30T13:02:57.621Z")
},
{
"_id" : ObjectId("5cc853ec0a5fde0e431898f3"),
"post" : "hey friends it's my final post",
"createdTime" : ISODate("2019-04-30T13:55:56.119Z")
}
],
"__v" : 0
}
//expected results is following
//display given objectId's posts array
"posts" : [
{
"_id" : ObjectId("5cc8478118195b71e2ccd517"),
"post" : "hey users",
"createdTime" : ISODate("2019-04-30T13:02:57.621Z")
},
{
"_id" : ObjectId("5cc853ec0a5fde0e431898f3"),
"post" : "hey friends it's my final post",
"createdTime" : ISODate("2019-04-30T13:55:56.119Z")
}
],
You just need to pass {posts:1}
Try this:
db.users.find({"_id" : ObjectId("5cc83a158ceb346aa58b1161")},{posts:1});
I am creating twitter clone using mongodb and want to search tweets which have certain hashtag in them. I came up with following document structure for my database -
{
"_id" : ObjectId("56f88c038c297eb4048e5dff"),
"twitterHandle" : "abhayp",
"firstName" : "Abhay",
"lastName" : "Pendawale",
"emailID" : "abhayp#gmail.com",
"password" : "278a2c36eeebde495853b14e6e5525fd12074229",
"phoneNumber" : "12394872398",
"location" : "San Jose",
"birthYear" : 1992,
"birthMonth" : 0,
"birthDay" : 1,
"followers" : [
"abhayp",
"deepakp",
"john",
"madhuras",
"nupurs",
"vgalgali"
],
"following" : [
"abhayp",
"abhinavk",
"ankitac",
"anupd",
"arpits"
],
"tweets" : [
{
"tweet_id" : "3f0fe01f8231356f784d07111efdf9d8ead28133",
"tweet_text" : "This new twitter sounds good!",
"created_on" : ISODate("2016-03-13T01:47:37Z"),
"firstName" : "Abhay",
"lastName" : "Pendawale",
"twitterHandle" : "abhayp",
"tags" : [ ]
},
{
"tweet_id" : "4e57b6d7d6b47d69054f0be55c238e8038751d84",
"tweet_text" : "#CMPE273 Node.js is #awesome",
"created_on" : ISODate("2016-03-07T23:16:39Z"),
"firstName" : "Abhay",
"lastName" : "Pendawale",
"twitterHandle" : "abhayp",
"tags" : [
"awesome",
"CMPE273"
]
},
{
"tweet_id" : "e5facd5f37c44313d5be02ffe0a3ca7190affd6b",
"tweet_text" : "Getting an incredible welcome in #Pune #travel #adventure #film #india ",
"created_on" : ISODate("2016-03-07T23:37:27Z"),
"firstName" : "Abhay",
"lastName" : "Pendawale",
"twitterHandle" : "abhayp",
"tags" : [
"adventure",
"film",
"india",
"Pune",
"travel"
]
},
{
"tweet_id" : "f5a735c1f747732f3e04f6cb2c092ff44750c0fd",
"tweet_text" : "The D Day today!\n#TheDDay",
"created_on" : ISODate("2016-03-18T22:24:57Z"),
"firstName" : "Abhay",
"lastName" : "Pendawale",
"twitterHandle" : "abhayp",
"tags" : [ ]
}
]}
I want to find out the tweets which have "Pune" in the tags array of the tweet. I tried following command
db.users.find({ "tweets.tags" : {$all: ["Pune"] }}, { tweets : 1 }).pretty();
but this command returns me ALL tweets of users who have 'Pune' entry in tags of one of their tweets. How can search only those tweets which have 'Pune' entry in their tags array?
Note: I don't want users who have tweeted #Pune, rather I want all tweets which contain #Pune. The duplicate marked question does not solve this problem.
Running following query -
db.users.aggregate([{
$match: {
'tweets.tags': 'Pune'
}
}, {
$project: {
tweets: {
$filter: {
input: '$tweets',
as: 'tweet',
cond: {
$eq: ['$$tweet.tags', 'Pune']
}
}
}
}
}]);
returns following results -
{ "_id" : ObjectId("56f88c038c297eb4048e5df1"), "tweets" : [ ] }
{ "_id" : ObjectId("56f88c038c297eb4048e5dff"), "tweets" : [ ] }
{ "_id" : ObjectId("56f88c038c297eb4048e5e07"), "tweets" : [ ] }
Which is certainly not what I want! :(
I don't think that's possible, beside using some convoluted aggregation stages.
The projection operator is close but only returns the first match.
My advice would be to put the tweets in a separate collection, that would be much more convenient to browse them, and probably more scalable. You're already duplicating the relevant user infos in them, so you don't have to change their content.