This question already has answers here:
Retrieve only the queried element in an object array in MongoDB collection
(18 answers)
Closed 3 years ago.
I tried to get embedded documents in the table using mongodb but its not working.
dev table
[{
"id":1,
"data":[{"id":1,"name":true},{"id":2,"name":true},{"id":3,"name":false},{"id":1,"name":true}]
}]
Query
db.dev,find({data.name:true})
Excepted output
[{
"id":1,
"data":[{"id":1,"name":true},{"id":2,"name":true}]
}]
I got Output
[{
"id":1,
"data":[{"id":1,"name":true},{"id":2,"name":true},{"id":3,"name":"false"},{"id":1,"name":true}]
}]
How to write the query to match the expected output. can give sample code
Try out this solution
let whereClause = { "data.name":true };
await db.dev.find(whereClause);
Related
This question already has answers here:
Insert an embedded document to a new field in mongodb document
(2 answers)
Closed 2 years ago.
My goal is to insert a field in my database as an object using shell as below.
reviewers: Object
reviewer1: "Alpha"
reviewer2: "Beta"
reviewer3: "Gamma"
I've tried several queries such as the following, but it entered the field as an array of object
db.movieDetails.update({"title": "The Martian"}, {$push:{"reviewers":{$each:[{"reviewer1":"Alpha"},{"reviewer2":"Beta"},{"reviewer3":"Gamma"}]}}})
What query would do exactly what I'm looking for?
Your input will be very helpful.
If you want to append the object to an existing one without remove the old fields you can use dot notation in this way:
db.collection.update({
"title": "The Martian"
},
{
"$set": {
"reviewers.reviewer1": "Alpha",
"reviewers.reviewer2": "Beta",
"reviewers.reviewer3": "Gamma"
}
})
Example here
But if you only want to set the entire object, then use $set with the object you want to insert into reviewers.
db.collection.update({
"title": "The Martian"
},
{
"$set": {
"reviewers": {
"reviewer1": "Alpha",
"reviewer2": "Beta",
"reviewer3": "Gamma"
}
}
})
Example here
How about
db.movieDetails.update({"title": "The Martian"}, {"reviewers":{"reviewer1":"Alpha","reviewer2":"Beta","reviewer3":"Gamma"}})
Edit: This will replace the entire document.
I tried the following:
db.regexPractice.update({"Employee id": 22}, {$set:{"reviewers":{"reviewer1":"Alpha","reviewer2":"Beta","reviewer3":"Gamma"}}}
And it worked correctly.
This question already has an answer here:
mongo .find return specific field only for all user [duplicate]
(1 answer)
Closed 2 years ago.
Following is the structure of the document i have in a collection in MongoDB
{
"_id": {
"$oid": "5f48e358d43721376c397f53"
},
"heading": "this is heading",
"tags": ["tag1","tag2","tag3"],
"categories": ["projA", "projectA2"],
"content": ["This", "is", "the", "content", "of", "the", "document"],
"timestamp": 1598612312.506219,
"lang": "en"
}
I only want to select ID and heading fields (like writing query "select id, heading from collection"). the projection entries are not working, I have tried a few different approaches after searching the internet. Filtering, sorting, grouping is working but projection is not.
How do i define projection in Mongodb input step in PDI to select certain fields from a collection I have tried specifying projection in query and field expression but it is not working.
Also i am having trouble concatenating string in the same step. so i would appreciate if someone can help me with that as well.
solved the problem in this step by specifying projection like this
[{$project: {heading: 1}}]
This question already has answers here:
Return result as an Array of Values Only
(2 answers)
Closed 3 years ago.
I can format my response into this, an array of objects of _ids:
[ { "_id": "5d703c09af11414e538fe95b" }, { "_id": "5d704b9b1ba3f75232d778d4" }, { "_id": "5d704bbe1ba3f75232d779de" } ]
However is it possible, within the aggregation pipeline, to transform it into just an array of the Object ids:
["5d703c09af11414e538fe95b", "5d704b9b1ba3f75232d778d4", "5d704bbe1ba3f75232d779de"]
I am aware that I could simply take the original response and map over it but I am unsure if we could do this directly within mongooose.
use $group operator as
{$group:{_id:null,ids:{$push:"$_id"}}}
this will return an array of ids
This question already has answers here:
How do I perform the SQL Join equivalent in MongoDB?
(19 answers)
MongoDB Aggregate lookup in Go (mgo.v2)
(1 answer)
Closed 4 years ago.
I have two collections.
I am using MongoDB version 3.4.
Collection 1 called Results
Collection 2 called Code_Value
Here i have to update automatically the code in the result using the Code_value Collections
The results is like:
{
_id: ObjectId(xxxxxxx)
name:"Mike"
job:"3"
salary:"4"
}
{
_id: ObjectId(xxxxxxx)
name:"Joe"
job:"1"
salary:"2"
}
the Code_value is like:
{
"Job":[
{"code":"1" , "value":"IT"}
{"code":"2" , "value":"Doctor"}
{"code":"3" , "value":"Developer"}
]
"Salary":[
{"code":"1" , "value":"900000"}
{"code":"2" , "value":"100000"}
{"code":"3" , "value":"200000"}
{"code":"4" , "value":"300000"}
]
}
I'd like to have a mapping code value on the results as the following:
{
_id: ObjectId(xxxxxxx)
name:"Mike"
job:"Developer"
salary:"300000"
}
{
_id: ObjectId(xxxxxxx)
name:"Joe"
job:"IT"
salary:"100000"
}
here is the query made as function
results.find().foreach(cursor){
v=Code_value.find(cursor,code)
if(v){
results.update(job,v.value)
}
This question already has answers here:
MongoDB: How to update multiple documents with a single command?
(13 answers)
FindAndModify, return array of Objects
(1 answer)
Closed 5 years ago.
I am trying to use mongoose's findByIdAndUpdate method to pass in a list of object id's and updating them at once. However, I am getting a "Error: Can't use $set with ObjectId." error which I can't seem to associate with my code.
Here's my code.
return ComponentModel.findByIdAndUpdate({
_id: {
$in: input.subjectIds
},
$set: { location: input.newLocation }
}).then(res => res);
findByIdAndUpdate is for one document. For multiple documents you can use update with multi flag true.
return ComponentModel.update(
{_id: {$in: input.subjectIds}},
{$set: {location: input.newLocation}},
{"multi": true}
).then(res => res);