mongodb query update select nested fields - mongodb

this is my document in mongo:
"calendar": {
"_id": "5cd26a886458720f7a66a3b8",
"hotel": "5cd02fe495be1a4f48150447",
"calendar": [
{
"_id": "5cd26a886458720f7a66a413",
"date": "1970-01-01T00:00:00.001Z",
"rooms": [
{
"_id": "5cd26a886458720f7a66a415",
"room": "5cd17d82ca56fe43e24ae5d3",
"price": "",
"remaining": 0,
"reserved": 0
},
{
"_id": "5cd26a886458720f7a66a414",
"room": "5cd17db6ca56fe43e24ae5d4",
"price": "",
"remaining": 0,
"reserved": 0
}
]
},
}
I need to update the objects in the inner rooms array . I tried a query that selects a matching element no syntax error but an error comes in:
"errmsg" : "The field 'calendar.0.rooms.0.price' must be an array but
is of type string in document {_id:
ObjectId('5cd26a886458720f7a66a3b8')}",
and this my query:
db.calendars.updateOne({_id:ObjectId("5cd26a886458720f7a66a3b8"),
"calendar":{"$elemMatch":{"_id":ObjectId("5cd26a886458720f7a66a413"),"rooms._id":
ObjectId("5cd26a886458720f7a66a415")}}},
{"$push":{"calendar.$[outer].rooms.$[inner].price":"100000"}}, {"arrayFilters":[{"outer._id":ObjectId("5cd26a886458720f7a66a413")},{"inner._id":ObjectId("5cd26a886458720f7a66a415")}]})
this is some reference I found in StackOverflow but not helped:
Updating a Nested Array with MongoDB

You can use below query
db.getCollection("test").updateOne(
{
"_id": ObjectId("5cd26a886458720f7a66a3b8"),
"calendar.calendar": {
"$elemMatch": {
"_id": ObjectId("5cd26a886458720f7a66a413"),
"rooms._id": ObjectId("5cd26a886458720f7a66a415")
}
}
},
{ "$set": { "calendar.calendar.$[outer].rooms.$[inner].price": "100000" } },
{
"arrayFilters": [
{ "outer._id": ObjectId("5cd26a886458720f7a66a413") },
{ "inner._id": ObjectId("5cd26a886458720f7a66a415") }
]
}
)
I will update my answer with some explanation afterward

Related

Delete objects that met a condition inside an array in mongodb

My collection has array "name" with objects inside. I need to remove only those objects inside array where "name.x" is blank.
"name": [
{
"name.x": [
{
"_id": "607e7fcca57aa56e2a06b57b",
"name": "abc",
"type": "123"
}
],
"_id": {
"$oid": "62232cd70ce38c5007de31e6"
},
"qty": "1.0",
"Unit": "pound,lbs"
},
{
"name.x": [
{
"_id": "607e7fcca57aa56e2a06b430",
"name": "xyz",
"type": "123"
}
],
"_id": {
"$oid": "62232cd70ce38c5007de31e7"
},
"qty": "1.0",
"Unit": "pound,lbs"
},{
"name.x": []
,
"_id": {
"$oid": "62232cd70ce38c5007de31e7"
},
"qty": "1.0",
"Unit": "pound,lbs"
}
I tried to get all the ids where name.x is blank using python and used $pull to remove objects base on those ids.But the complete array got deleted.How can I remove the objects that meet the condition.
Think MongoDB update with aggregation pipeline meets your requirement especially to deal with the field name with ..
$set - Update the name array field by $filter name.x field is not an empty array.
db.collection.update({},
[
{
$set: {
name: {
$filter: {
input: "$name",
cond: {
$ne: [
{
$getField: {
field: "name.x",
input: "$$this"
}
},
[]
]
}
}
}
}
}
],
{
multi: true
})
Sample Mongo Playground

Add field to every object in array of objects in mongo aggregation

I have one field in schema at root level and want to add it in every object from array which matches a condition.
Here is a sample document....
{
calls: [
{
"name": "sam",
"status": "scheduled"
},
{
"name": "tom",
"status": "cancelled"
},
{
"name": "bob",
"status": "scheduled"
},
],
"time": 1620095400000.0,
"call_id": "ABCABCABC"
}
Required document is as follows:
[
{
"call_id": "ABCABCABC",
"calls": [
{
"call_id": "ABCABCABC",
"name": "sam",
"status": "scheduled"
},
{
"name": "tom",
"status": "cancelled"
},
{
"call_id": "ABCABCABC",
"name": "bob",
"status": "scheduled"
}
],
"time": 1.6200954e+12
}
]
The call_id should be added to all objects in array whose status is "scheduled".
Is it possible to do this with mongo aggregation? I have tried $addFields but was unable to achieve above result.
Thanks in advance!
Here is how I would do it using $map and $mergeObjects
db.collection.aggregate([
{
"$addFields": {
calls: {
$map: {
input: "$calls",
as: "call",
in: {
$cond: [
{
$eq: [
"$$call.status",
"scheduled"
]
},
{
"$mergeObjects": [
"$$call",
{
call_id: "$call_id"
}
]
},
"$$call"
]
}
}
}
}
}
])
Mongo Playground

Fetching all Grandchild's from single collection using Self join in mongoose

Currently i have one collection "Category" which has a self join with ref on parent column. In that I have 3 Document.
Document 1 is the parent.
Document 2 is the child of Document 1
Document 3 is the child of Document 2
CategoryModel
var CategoryTable = new Schema({
categoryName : String,
parent : {
type : ObjectID,
ref : 'category',
default : null
}
});
MyCode
CategoryModel
.aggregate([
{
$match : { "parent" : null }
},
{
"$lookup":{
"from" : "categories",
"localField":"_id",
"foreignField":"parent",
"as": "child"
}
}
])
.exec((err,data) => {
if(err)
{
throw err;
}
res.send(data);
})
Current Output
[
{
"_id": "5d8de924b4672e2744dedbb9",
"parent": null,
"categoryName": "Software",
"__v": 0,
"child": [
{
"_id": "5d8de972b4672e2744dedbba",
"parent": "5d8de924b4672e2744dedbb9",
"categoryName": "Antivirus",
"__v": 0
}
]
}
]
Expected Output
[
{
"_id": "5d8de924b4672e2744dedbb9",
"parent": null,
"categoryName": "Software",
"__v": 0,
"child": [
{
"_id": "5d8de972b4672e2744dedbba",
"parent": "5d8de924b4672e2744dedbb9",
"categoryName": "Antivirus",
"__v": 0,
"child": [
{
"_id": "5d8e1a303bcfb6085c48e4dc",
"parent": "5d8de972b4672e2744dedbba",
"categoryName": "Quick Heal",
"__v": 0
}
]
}
]
}
]
I want to fetch all the child and subchild collection using aggregate. Can someone please help me in this?

Aggregate query result in mongodb

I have collection with documents like this one:
{
"_id": 1,
"people": [
{
"name": "Bob",
"age": "15"
},
{
"name": "Alice",
"age": "18"
}
]
}
My query is:
db.groups.aggregate({ $match: { "_id": 1 }}, { $project: { "_id": 0, "people.name": 1 } })
This query returns:
{
"people": [
{
"name": "Bob"
},
{
"name": "Alice"
}
]
}
But I need the result like:
{ "names": [ "Bob", "Alice" ] }
Which parameters should I add to the .aggregate() function?
The solution is:
db.groups.aggregate({ $match: { "_id": 1 }}, { $project: { "_id": 0, "names": "$people.name" } })

Fetch mongo documents based on multiple fields

Given mongo document of the following form in a collection:
{
"_id":"ObjectId",
"value":{
"id": 1,
"payment": [
{
"status": {
"id": "1.1",
"value": "Paid"
}
},
{
"status": {
"id": "1.2",
"value": "Scheduled"
}
},
{
"status": {
"id": "1.3",
"value": "Recorded"
}
}
]
}
}
ids = [1,2,3,4]
How can i fetch all documents having id in ids and at least one of payments.status.value equal to Scheduled state ?
I am using the following query but it's returning 0 records,
db.collectionName.find({$and:[{"value.id":{$in:ids}},{"value.payment.status.value":"Scheduled"}]})`
you can specify the name of the collection
so instead of this:
db.collection.find({
$and:
[
{"value.id": {$in:ids}},
{"value.payment.status.value":"Scheduled"}
]
})
you can write:
db.payments.find({
$and:
[
{"value.id":{$in:ids}},
{"value.payment.status.value":"Scheduled"}
]
})
db.collection.find({
'value.payment': {
$elemMatch: {
'status.value': 'Scheduled'
}
},
'value.id': {
$in: [1, 2, 3, 4]
}
}, {
'value.id': 1,
'value.payment.$.status': 1
})