mongodb fetch records if array exists - javascript

I have 2 collections:
Vehicles:
[
{
"_id": "a1",
"type:": "car",
"make": "Honda",
"specifications": ["1", "2"]
},
{
"_id": "a2",
"type:": "car",
"make": "Toyota",
"specifications": ["3", "4"]
},
{
"_id": "a3",
"type:": "car",
"make": "Honda",
"specifications": []
},
{
"_id": "a4",
"type:": "car",
"make": "Toyota"
}
]
Specifications:
[
{
"_id": "1",
"color": "Black"
},
{
"_id": "2",
"sunroof": "yes"
},
{
"_id": "3",
"engine": "1800 CC"
},
{
"_id": "4",
"bodyType": "Sedan"
}
]
I want to fetch those records which has at least one specification.
And also the details from specifications collections should appear in Vehicles collections somewhere.
Expected response:
[
{
"_id": "a1",
"make": "Honda",
"type:": "car",
"carSpecifications": [
{
"color": "Black"
},
{
"sunroof": "yes"
}
],
},
{
"_id": "a2",
"make": "Toyota",
"type:": "car",
"specifications": [
{
"engine": "1800 CC"
},
{
"bodyType": "Sedan"
}
]
}
]
Now what I tried so far is:
db.vehicles.find({type: "car", "specifications": {$exists: true}}, {fields: {"specifications.$": 1}}).fetch()
this query is returning all the records from Vehicles.
After getting all the records I put a loop on the records I get and check manually if specifications.length > 0 than I query from Specifications collection accordingly.
Can I achieve all this with a single query?

You should look for an aggregation query.
$match - Filter documents with "type:" "car" and specifications is not an empty array (with $ifNull, default as [] when specifications field is null or not existed).
$lookup - Vehicles collection join specifications collection (Refer to Use $lookup with an Array). Work with pipeline to return the array without the _id field (Refer to Correlated Subqueries Using Concise Syntax).
MongoDB v5 query
db.vehicles.aggregate({
$match: {
$and: [
{
"type:": "car"
},
{
$expr: {
$ne: [
{
$ifNull: [
"$specifications",
[]
]
},
[]
]
}
}
]
}
},
{
$lookup: {
from: "specifications",
localField: "specifications",
foreignField: "_id",
pipeline: [
{
$project: {
_id: 0
}
}
],
as: "specifications"
}
})
Sample Mongo Playground (v5)
MongoDB v4 query
db.vehicles.aggregate({
$match: {
$and: [
{
"type:": "car"
},
{
$expr: {
$ne: [
{
$ifNull: [
"$specifications",
[]
]
},
[]
]
}
}
]
}
},
{
$lookup: {
from: "specifications",
let: {
specifications: "$specifications"
},
pipeline: [
{
$match: {
$expr: {
$in: [
"$_id",
"$$specifications"
]
}
}
},
{
$project: {
_id: 0
}
}
],
as: "specifications"
}
})
Sample Mongo Playground (v4)

Related

Converting string to Array (Aggregate mongodb)

Im getting random data from my database using the aggregate but apparently some of them have their genre values in string not in array, How can i convert those random data genre from string to array?
const book = await Book.aggregate([
/* This is just for testing which books has genres in specific type
{
$match: { genre: { $type: "string" } }
},
*/
{
$sample: { size: 6 }
}
]);
sample data result(genres that are in type of string)
[
{
"_id": "62710ac63ad1bfc6d17030fe",
"title": "Birth of a Theorem",
"author": "Villani, Cedric",
"genre": "mathematics",
"publisher": "Bodley Head",
"dateOfPublication": "2002-02-28T00:00:00.000Z",
"noOfCopies": 16,
"type": "Book",
"form": "Non-fiction",
"isbn": "979-81202-479229-867673-6",
"dateAdded": "2002-11-28T00:00:00.000Z"
},
{
"_id": "62710ac63ad1bfc6d1703108",
"title": "All the President's Men",
"author": "Woodward, Bob",
"genre": "history",
"publisher": "Random House",
"dateOfPublication": "2018-02-19T00:00:00.000Z",
"noOfCopies": 56,
"type": "Book",
"form": "Non-fiction",
"isbn": "978-41428-6606587-937631-",
"dateAdded": "2011-02-23T00:00:00.000Z"
},
]
sample data result(genres that are in type of array)
[
{
"_id": "62710ac63ad1bfc6d17030be",
"title": "Superfreakonomics",
"author": "Dubner, Stephen",
"genre": [
"economics",
"computer_science"
],
"publisher": "HarperCollins",
"dateOfPublication": "2003-10-06T00:00:00.000Z",
"noOfCopies": 31,
"type": "Thesis",
"form": "Non-fiction",
"isbn": "978-35029-7186192-465859-7",
"dateAdded": "2009-02-12T00:00:00.000Z"
}
]
$sample is for documents instead of embedded array.
Update
db.collection.update({
genre: {
$type: "string"
}
},
[
{
$set: { genre: [ "$genre" ] }
}
],
{
multi: true
})
mongoplayground
Aggregate
db.collection.aggregate([
{
$match: {
genre: {
$type: "string"
}
}
},
{
$set: {
genre: {
$cond: {
if: { $eq: [ { $type: "$genre" }, "string" ] },
then: [ "$genre" ],
else: "$genre"
}
}
}
}
])
mongoplayground

MongoDB Aggregate $lookup on array of object with additional field

I have two different collections (example below) methods & items. As for now, I'm using pre 3.6 vanilla aggregation query for $lookup:
MongoPlayground Example
{
$lookup: {
from: "items",
localField: "reagents._id",
foreignField: "_id",
as: "reagent_items"
}
}
The problem is that if I am using it, I miss quantity field (from methods.reagents embedded) during $lookup stage from original collection. For now, I return quantity right after lookup but as I heard, Mongo introduced from 3.6 a new syntax for lookup queries, so the question is:
Can it solve my problem for receiving the following results:
{
"_id": 1,
"name": "Test",
"reagent_items": [ // <= the exact schema of what I need after lookup
{
"_id": 1,
"name": "ItemOne",
"other": "field",
"quantity": 2 //quantity field from original {array of objects} after lookup
},
{
"_id": 2,
"name": "ItemTwo",
"other": "field",
"quantity": 4 //quantity field from original {array of objects} after lookup
}
],
"reagents": [ //original reagents field here just for example, we could remove it
{
"_id": 1,
"quantity": 2
},
{
"_id": 2,
"quantity": 4
}
]
}
methods
{
"_id": 1,
"name": "Test",
"reagents": [
{
_id: 1,
quantity: 2
},
{
_id: 2,
quantity: 4
}
]
}
items
{
"_id": 1,
"name": "ItemOne",
"other": "field"
},
{
"_id": 2,
"name": "ItemTwo",
"other": "field"
}
Use $map along with $arrayElemAt to find corresponding reagent for each reagent_items and the apply $mergeObjects to get one object:
db.methods.aggregate([
{
$lookup: {
from: "items",
localField: "reagents._id",
foreignField: "_id",
as: "reagent_items"
}
},
{
$project: {
_id:1,
name: 1,
reagents: 1,
reagent_items: {
$map: {
input: "$reagent_items",
as: "ri",
in: {
$mergeObjects: [
"$$ri",
{
$arrayElemAt: [ { $filter: { input: "$reagents", cond: { $eq: [ "$$this._id", "$$ri._id" ] } } }, 0 ]
}
]
}
}
}
}
}
])
Mongo Playground

How to findOneAndUpdate single field with multi nested array documents

I'm stuck on how to update single value in multi nested array documents value with findOneAndUpdate.
My condition goes like this:
Update warehouse amount where the productCode is "abc123", size "41" in warehouse "Hamburg".
I just get back null or bot sizes 41 and 42.
Here is the part of the doc:
{
"_id": ObjectId("xxxx636309f84479ec0c7b"),
"productCode": "abc123",
"brand": "Nike",
"name": "aaa",
"model": "Runner",
"color": "Brown",
"image": "shoe.jpg",
"sizes": [{
"_id": ObjectId("xxxxc636309f84479ec0c7e"),
"size": "41",
"wares": [{
"_id": ObjectId("xxxx2c636309f84479ec0c80"),
"ware": "Hamburg",
"amount": 7
},
{
"_id": ObjectId("5db72c636309f84479ec0c7f"),
"ware": "Berlin",
"amount": 7
}
]
},
{
"_id": ObjectId("5db72c636309f84479ec0c7c"),
"size": "42",
"wares": [{
"_id": ObjectId("5db72c636309f84479ec0c7d"),
"ware": "Hamburg",
"amount": 16
}]
}
],
"__v": 0
}
This is what I've tried:
Product.findOneAndUpdate({
"productCode": "abc123",
"sizes.size": 41,
"sizes.wares.ware": "Hamburg"
}, {
"$set": {
"sizes.0.wares.amount": 99
}
}, {
useFindAndModify: false
},
(err, products) => {
if (err) {
return res.status(422).send(err)
}
return res.json(products)
}
);
How can I solve this?
And to fulfill #ambianBeing, this is how it would be done with findOneAndUpdate:
Product.findOneAndUpdate({
"productCode": "abc123",
"sizes": {
$elemMatch: {
$and: [
{ size: "41" },
{
wares: {
$elemMatch: {
ware: "Hamburg"
}
}
}]
}
}
}, {
$set: {
"sizes.$[theSize].wares.$[theWare].amount": 99
}
}, {
arrayFilters: [{
"theSize.size": "41"
}, {
"theWare.ware": "Hamburg"
}]
})
Can be done using filtered positional operator $[<identifier>] which is nifty in use cases of nested array updation.
Query (Mongo Shell):
db.collection.update(
{ productCode: "abc123" },
{ $set: { "sizes.$[outer].wares.$[inner].amount": 99 } },
{
arrayFilters: [{ "outer.size": "41" }, { "inner.ware": "Hamburg" }],
multi: false
}
);
Query with Mongoose Model:
Product.update(
{ productCode: "abc123" },
{ "sizes.$[outer].wares.$[inner].amount": 99 },
{
arrayFilters: [{ "outer.size": "41" }, { "inner.ware": "Hamburg" }],
multi: false
},
(err, rawDoc) => {
if (err) {
console.error(err);
}
console.info(rawDoc);
}
);

$filter upto 3 nested level in mongodb

I have a below collection
[
{
"Array1": [
{
"Array2": [
{
"name": "6666",
"Array3": [
{ "_id": 128938120, "nest": "samsung" },
{ "_id": 12803918239, "nest": "nokia" }
]
},
{
"name": "5555",
"Array3": [
{ "_id": 48102938109, "nest": "iphone" },
{ "_id": 501293890, "nest": "micromax" }
]
}
],
"name": "old apartment"
},
{
"Array2": [
{
"_id": 410923810,
"name": "3333",
"Array3": [
{ "_id": 48102938190, "nest": "airtel" },
{ "_id": 48102938190, "nest": "jio" }
]
},
{
"_id": 41092381029,
"name": "2222",
"Array3": [
{ "_id": 10293182309, "nest": "master" },
{ "_id": 38190238, "nest": "cub" }
]
}
],
"name": "new apartment"
}
]
}
]
I want to fo $filter to 3 nested level... I want only nokia element from 3rd array and 6666 element from second and old apartment from the first
I want this output
[
{
"Array1": [
{
"Array2": [
{
"name": "6666",
"Array3": [
{
"_id": 12803918239,
"nest": "nokia"
}
]
}
],
"name": "old apartment"
}
]
}
]
And also I want to do using $filter and $map only... Don't want to use $unwind here
Basically you have to use $filter for each level to apply your condition and $map for each array that has nested array inside. That's because you want to pass filtered array to the output. So there will be 3 filters and 2 maps. Indentations might be really helpful in this case. Try:
db.collection.aggregate([
{
$project: {
Array1: {
$map: {
input: { $filter: { input: "$Array1", as: "a1", cond: { $eq: ["$$a1.name", "old apartment" ] } } },
as: "a1",
in: {
name: "$$a1.name",
Array2: {
$map: {
input: { $filter: { input: "$$a1.Array2", as: "a2", cond: { $eq: [ "$$a2.name", "6666" ] } } },
as: "a2",
in: {
name: "$$a2.name",
Array3: { $filter: { input: "$$a2.Array3", as: "a3", cond: { $eq: [ "$$a3.nest", "nokia" ] } } }
}
}
}
}
}
}
}
}
])
Mongo playground

Retrieving value from Mongodb query response

I have a mongodb aggregation framework query as shown below.I am unable to parse the output of the below query
myModel.aggregate(
[
{
"$match": { "$and": [{ "serviceActiveFlag": "Y" }, { "hospitalName": hospitalName }] }
},
//decompile array
{ $unwind: "$Treatment" },
{
$group: {
_id: "$Treatment.departmentName", "procedureList": {
$push: { "procedureName": "$Treatment.name", "cost": "$Treatment.costLowerBound" }
}
}
},
{
$project: {
"_id": 0,
"department": '$_id',
"procedureList": 1
}
}
], function (err, result) {
})
Output of the above query is shown below
{
"data": [
{
"procedureList": [
{
"procedureName": "Root Canal",
"cost": 10200
}
],
"department": "Dental"
},
{
"procedureList": [
{
"procedureName": "Bone Grafting",
"cost": 20000
}
],
"department": "Ortho"
}
]
}
How do i retrieve the value corresponding to key data?
I tried result.data[0],but I got undefined error
Expected output is given below
Expected output
[
{
"procedureList": [
{
"procedureName": "Root Canal",
"cost": 10200
}
],
"department": "Dental"
},
{
"procedureList": [
{
"procedureName": "Bone Grafting",
"cost": 20000
}
],
"department": "Ortho"
}
]
I think you forgot to parse the json-data, that's why it isn't able to index, Please use the code below to parse the json and then try accessing the array. if you encounter any problems, please mention them.
result = JSON.parse(result)
result.data[0]
Result is an object so,
let data = result.data

Categories