Update a nested array objects in a different collection and position in MongoDB - mongodb

I have a douments like as follows.
How do I update a skillcluster name. Suppose the other document has name :"c" in 4th position.
{
Job: {
post: { name:"x" }
skill: {
skillcluster: [
{name:"c++",id:"23"},
{name:"c",id:"898"}
]
}
}
}
{
Job: {
post: { name:"x" }
skill: {
skillcluster: [
{name:"c++",id:"23"},
{name:"java"},
{name:"python"},
{name:"c",id:"898"}
]
}
}
}

You need to query to match the "name" field at the embedded level of the document using "dot notation", and then pass that match with the positional $ operator within the update:
db.collection.update(
{ "Job.skill.skillcluster.name": "c" },
{ "$set": { "Job.skill.skillcluster.$.name": "Simple C"}},
{ "multi": true }
)
Also use the "multi" flag to match and update more than one document.
The result will be:
{
"_id" : ObjectId("55dbfd0ed96d655eb0ed2b4f"),
"Job" : {
"post" : {
"name" : "x"
},
"skill" : {
"skillcluster" : [
{
"name" : "c++",
"id" : "23"
},
{
"name" : "Simple C",
"id" : "898"
}
]
}
}
}
{
"_id" : ObjectId("55dbfd0ed96d655eb0ed2b50"),
"Job" : {
"post" : {
"name" : "x"
},
"skill" : {
"skillcluster" : [
{
"name" : "c++",
"id" : "23"
},
{
"name" : "java"
},
{
"name" : "python"
},
{
"name" : "Simple C",
"id" : "898"
}
]
}
}
}

Related

MongoDB join two collections with between clause

There is a collection "Printers":
{
"_id" : ObjectId("5cc02f9b9931de72296ba6c2"),
"model" : "Xerox WorkCentre 3315",
"serial" : "3255498494",
"date" : ISODate("2019-04-25T08:57:48.001+0000"),
"pages" : NumberInt(4868),
"location" : "New location",
"ip" : "10.159.0.35",
"ip_int" : NumberInt(178192419)
}
and "Branches" collection:
{
"_id" : ObjectId("5cb4799b8c0cfe35e4a4c266"),
"name" : "Office 1",
"ip_start" : NumberLong(178192384),
"ip_end" : NumberLong(178194431)
}
// ----------------------------------------------
{
"_id" : ObjectId("5cb479e68c0cfe35e4a4c269"),
"name" : "Office 2",
"ip_start" : NumberLong(3232258048),
"ip_end" : NumberLong(3232258303)
}
"Branches" collection contains ip addresses converted into integer value, i.e. 192.168.0.1 is 3232235521. Each record in Branches describes subnet.
Each printer located in one branch.
If printers.ip_int between branches record [ip_start;ip_end] then query should return all fields from Printer and one field "Name" from "Branches" collection.
How can i do this?
You need a lookup with custom pipeline where you can specify "between" condition:
db.Branches.aggregate([
{
$lookup: {
from: "Printers",
let: { ip_start: "$ip_start", ip_end: "$ip_end" },
pipeline: [
{
$match: {
$expr: {
$and: [
{ "$gte": [ "$ip_int", "$$ip_start" ] },
{ "$lte": [ "$ip_int", "$$ip_end" ] },
]
}
}
}
],
as: "Printers"
}
}
])
db.getCollection("printers").aggregate(
[
{
"$lookup" : {
"from" : "branches",
"let" : {
"ip_int" : "$ip_int"
},
"pipeline" : [
{
"$match" : {
"$expr" : {
"$and" : [{"$gte" : ["$$ip_int", "$ip_start"]},
{ "$lte" : ["$$ip_int", "$ip_end"]}
]
}
}
}
], "as" : "Printers"
}
},
{
"$sort" : {
"ip_int" : 1.0
}
},
{
"$unwind" : {
"path" : "$Printers"
}
},
{
"$addFields" : {
"filial" : "$Printers.name"
}
},
{
"$project" : {
"Printers" : false, "ip_int" : false
}
}
]);

Unwind children array of objects

I have an array of objects in a collection. I show two documents to show the structure of these array.
The property "tipos" is an array of objects. And inside this child array, the property "ingredientes" is another array of objects.
{
"_id" : ObjectId("5c6c32337acdd946b66f76e9"),
"name" : "Ensaladas",
"tipos" : [
{
"name" : "Rusa",
},
{
"name" : "Cesars",
"ingredientes" : [
{
"name" : "Lechuga",
"amount" : 20
},
{
"name" : "Vinagreta",
"amount" : 10
}
]
},
{
"name" : "Campesina",
"ingredientes" : [
{
"name" : "Beterraga",
"amount" : 55
}
]
}
]
},
{
"_id" : ObjectId("5c6c32337acdd946b66f76e9"),
"name" : "Carnes",
"tipos" : [
{
"name" : "Estofado de pollo",
},
{
"name" : "Lomo saltado",
"ingredientes" : [
{
"name" : "Lomo fino",
"amount" : 50
},
{
"name" : "Tomate",
"amount" : 15
}
]
}
]
}
I need to unwind it enough to get this result:
Ensaladas Rusa
Ensaladas Cesars Lechuga
Ensaladas Cesars Vinagreta
Ensaladas Campesina Beterraga
Carnes Estofado de pollo
Carnes Lomo saltado Lomo fino
Carnes Lomo saltado Tomate
I have been trying double unwind but not getting the result I need.
Thanks.
You need to use $unwind with preserveNullAndEmptyArrays set to true since not all the documents contain tipos.ingredientes path. Then you can use $concat with $rtrim to build the name as single string, try:
db.col.aggregate([
{ $unwind: "$tipos" },
{ $unwind: { path: "$tipos.ingredientes", preserveNullAndEmptyArrays: true } },
{ $project: { _id: 0, name: { $rtrim: { input: { $concat: [ "$name", " ", "$tipos.name", " ", { $ifNull: [ "$tipos.ingredientes.name", "" ] } ] } } } } }
])
Outputs:
{ "name" : "Ensaladas Rusa" }
{ "name" : "Ensaladas Cesars Lechuga" }
{ "name" : "Ensaladas Cesars Vinagreta" }
{ "name" : "Ensaladas Campesina Beterraga" }
{ "name" : "Carnes Estofado de pollo" }
{ "name" : "Carnes Lomo saltado Lomo fino" }
{ "name" : "Carnes Lomo saltado Tomate" }

Meteor MongoDB return single object

{
"_id" : "R9zbu8oTWwgxT5eCR",
"practiceSetup" : {
"operatoriesSettings" : [
{
"name" : "Operatory 1",
"description" : "Room 1",
"status" : true
},
{
"name" : "Operatory 2",
"description" : "Room B",
"status" : true
}
],
}
I'm trying to lookup object that has name "Operatory 1" and only return that object.
Here is what I have tried,
Practice.findOne({ _id: 'R9zbu8oTWwgxT5eCR' }, { "$elemMatch": { "practiceSetup.operatoriesSettings": { "name": "Operatory 1" } } } );
However this will return the whole document, not that particular object, not sure what I'm doing wrong here.
You can try out this,
Practice.findOne({ _id: 'R9zbu8oTWwgxT5eCR' }).map(function(u){
return u.practiceSetup
.operatoriesSettings
.filter( m => m.name == "Operatory 1");
})
This will return:
[
[
{
"name" : "Operatory 1",
"description" : "Room 1",
"status" : true
}
]
]
That's how $elemMatch projection works:
The $elemMatch operator limits the contents of an field from the query results to contain only the first element matching the $elemMatch condition.
If you only need that specific subdocument, you get it using JS:
const doc = Practice.findOne(
{ _id: 'R9zbu8oTWwgxT5eCR' },
{
$elemMatch: {
'practiceSetup.operatoriesSettings': {
name: 'Operatory 1'
}
}
}
);
const operator1 = doc.practiceSetup.operatoriesSettings[0];
I found that using "Projections" will return matched object off an array.
Query:
{ "_id": "R9zbu8oTWwgxT5eCR", "practiceSetup.operatoriesSettings": { $elemMatch: {'name': 'Operatory 1'} } }
Projection
{"practiceSetup.operatoriesSettings.$.name": 1}
Full Query:
db.Practice.findOne({
"_id" : "R9zbu8oTWwgxT5eCR",
"practiceSetup.operatoriesSettings" : {
"$elemMatch" : {
"name" : "Operatory 1"
}
}
}, {
"practiceSetup.operatoriesSettings.$.name" : 1.0
});
Returns
{
"_id" : "R9zbu8oTWwgxT5eCR",
"practiceSetup" : {
"operatoriesSettings" : [
{
"name" : "Operatory 1",
"description" : "Room 1",
"status" : true
}
]
}
}
Meteor
When using Meteor methods, you will need to use "fields" when using Projections.
return Practice.findOne({
"_id": "R9zbu8oTWwgxT5eCR",
"practiceSetup.operatoriesSettings": {
"$elemMatch": {
"name": data
}
}
},
{ fields: { 'practiceSetup.operatoriesSettings.$.name': 1, _id: 0 } }
);

update specific map with multiple condition in mongo array

{
"dictID" : "37528e10-6344-4d93-8a57-35af0bdb6b34",
"dictVersion" : 1,
"addMeasures" : [
{
"measureId" : "f229ba18-0de8-47a1-8a87-88c95edd536a",
"userId" : "3966C3DD-8328-4F18-A061-02A01111763A",
"created" : "2017-01-07T05:47:22.512Z",
"ownerAction" : "NA"
}
],
"deleteMeasures" : [
{
"measureId" : "0b701469-1502-4de4-95de-1ee70ad6c577",
"userId" : "3966C3DD-8328-4F18-A061-02A01111763A",
"created" : "2017-01-07T05:47:35.193Z",
"ownerAction" : "NA"
},
{
"measureId" : "443d1b97-95ae-4410-9302-da3edbad4004",
"userId" : "3966C3DD-8328-4F18-A061-02A01111763A",
"created" : "2017-01-07T05:47:36.062Z",
"ownerAction" : "NA"
},
{
"measureId" : "aa1689c9-8df6-4bff-88a4-274a4f2dc2aa",
"userId" : "3966C3DD-8328-4F18-A061-02A01111763A",
"created" : "2017-01-07T05:47:37.075Z",
"ownerAction" : "NA"
}
]
}
Above is my mongo collection and i want to do the following query:
db.getCollection('DataDictionaryReview').update(
{
$and: [
{ dictID: "37528e10-6344-4d93-8a57-35af0bdb6b34" },
{ dictVersion: 1 },
{ "deleteMeasures.measureId": "aa1689c9-8df6-4bff-88a4-274a4f2dc2aa" },
{ "deleteMeasures.ownerAction": "NA" }
]
},
{
$set: {
"deleteMeasures.$.created": "2017-01-07T06:51:56.983Z",
"deleteMeasures.$.ownerAction": "Reject"
}
}
)
I want to update the last map of deleteMeasures but every time the query is updating first map of deleteMeasures.
This is strange, the query looks okay to me, however I think it needs $elemMatch to get the correct index value.
db.getCollection('DataDictionaryReview').update(
{
dictID: "37528e10-6344-4d93-8a57-35af0bdb6b34",
dictVersion: 1 ,
"deleteMeasures" : { $elemMatch : {"measureId": "aa1689c9-8df6-4bff-88a4-274a4f2dc2aa", "ownerAction": "NA" }}
},
{
$set: {
"deleteMeasures.$.created": "2017-01-07T06:51:56.983Z",
"deleteMeasures.$.ownerAction": "Reject"
}
}
)

how to get this query in mongoDB

I have this collection...
> db.banks.find().pretty()
{
"_id" : ObjectId("54f37cbb44aec3b01b7db8f4"),
"name" : "A",
"branches" : [
{
"branch_id" : 8561,
"name" : "X",
},
{
"branch_id" : 8576,
"name" : "Y",
}
]
}
{
"_id" : ObjectId("54f37cbb44aec3b01b7db8f5"),
"name" : "B",
"branches" : [
{
"branch_id" : 3238,
"name" : "Z",
}
]
}
with this command :
db.banks.aggregate({$project{"branches.name":1,"_id":0}});
get this result :
{ "branches" : { { "name" : "X" }, { "name" : "Y" } } }
{ "branches" : { { "name" : "Z" } } }
but; how I get this result?
(In fact, one object and without "branches".)
{{"name" : "X"}, {"name" : "Y"}, {"name" : "Z"}}
very thanks...
One way you could go about this is to do an $unwind first in the aggregation pipeline to get a deconstructed array with a document for each element and then group by the array element $branches.name:
db.banks.aggregate([
{ $unwind: '$branches'},
{
$group: {
_id: {
name: '$branches.name'
}
}
},
{
$project: {
_id: 0,
name: '$_id.name'
}
},
{ $sort : { "name" : 1 } }
])
Outputs:
{
"result" : [
{
"name" : "X"
},
{
"name" : "Y"
},
{
"name" : "Z"
}
],
"ok" : 1
}