MongoDB Aggregate Query to find the documents with missing values - mongodb

I am having a huge collection of objects where the data is stored for different employees.
{
"employee": "Joe",
"areAllAttributesMatched": false,
"characteristics": [
{
"step": "A",
"name": "house",
"score": "1"
},
{
"step": "B",
"name": "car"
},
{
"step": "C",
"name": "job",
"score": "3"
}
]
}
There are cases where the score for an object is completely missing and I want to find out all these details from the database.
In order to do this, I have written the following query, but seems I am going wrong somewhere due to which it is not displaying the output.
I want the data in the following format for this query, so that it is easy to find out which employee is missing the score for which step and which name.

db.collection.aggregate([
{
"$unwind": "$characteristics"
},
{
"$match": {
"characteristics.score": {
"$exists": false
}
}
},
{
"$project": {
"employee": 1,
"name": "$characteristics.name",
"step": "$characteristics.step",
_id: 0
}
}
])
You need to use $exists to check the existence
playground

You can use $ifNull to handle both cases of 1. the score field is missing 2. score is null.
db.collection.aggregate([
{
"$unwind": "$characteristics"
},
{
"$match": {
$expr: {
$eq: [
{
"$ifNull": [
"$characteristics.score",
null
]
},
null
]
}
}
},
{
"$group": {
_id: null,
documents: {
$push: {
"employee": "$employee",
"name": "$characteristics.name",
"step": "$characteristics.step",
}
}
}
},
{
$project: {
_id: false
}
}
])
Here is the Mongo playground for your reference.

Related

MongoDB returning two array while using alias

I am a json in mongodb. The structure is below --
{
id: "1",
name: "sample",
user: [
{
data_alias: "ex",
value: "efg"
}
]
}
Now I want the the data_alias to be data after mongodb returns the result.
When I am using below query --
db.coll.find(
{"id":"1"},
{"data": "$user.data_alias","_id": 0,"value":1}
)
Now it is retuning data like --
{
"user": [
{
"value": "efg",
},
],
"data": [
"ex"
]
}
But I want the returning value should be like --
{
"data": "ex",
"name": "sample"
}
Also I have tried with aggregate function
db.colls.aggregate([
{
$match: {
"id": "1"
}
},
{
"$project": {
"_id": 0,
"data": "$user.data_alias"
}
}
]);
Both the queries returning same result.
Just $unwind the user array and $project to your expected output.
db.collection.aggregate([
{
$match: {
name: "sample"
}
},
{
"$unwind": "$user"
},
{
"$project": {
_id: 0,
"name": 1,
"data": "$user.data_alias"
}
}
])
Here is the Mongo playground for your reference.

How to count embedded array object elements in mongoDB

{
"orderNo": "123",
"bags": [{
"type": "small",
"products": [{
"id": "1",
"name": "ABC",
"returnable": true
}, {
"id": "2",
"name": "XYZ"
}
]
},{
"type": "big",
"products": [{
"id": "3",
"name": "PQR",
"returnable": true
}, {
"id": "4",
"name": "UVW"
}
]
}
]
}
I have orders collection where documents are in this format. I want to get a total count of products which has the returnable flag. e.g: for the above order the count should be 2. I am very new to MongoDB wanted to know how to write a query to find this out, I have tried few things but did not help:
this is what I tried but not worked:
db.orders.aggregate([
{ "$unwind": "$bags" },
{ "$unwind": "$bags.products" },
{ "$unwind": "$bags.products.returnable" },
{ "$group": {
"_id": "$bags.products.returnable",
"count": { "$sum": 1 }
}}
])
For inner array you can use $filter to check returnable flag and $size to get number of such items. For the outer one you can take advantage of $reduce to sum the values from inner arrays:
db.collection.aggregate([
{
$project: {
totalReturnable: {
$reduce: {
input: "$bags",
initialValue: 0,
in: {
$add: [
"$$value",
{
$size: {
$filter: {
input: "$$this.products",
as: "prod",
cond: {
$eq: [ "$$prod.returnable", true ]
}
}
}
]
}
}
}
}
}
}
])
Mongo Playground

mongodb update multiple Array elements using db.update()

I compiled 2 update queries by referring to related stackoverflow answers, however, it doesn't seem to work, query updates all elements while only elements matching the criteria are expected to update.
Document:
[
{
"_id": 259,
"members": [
{
"email": "test1#gmail.com",
"added_by": "javapedia.net",
"status": "pending"
},
{
"email": "test2#gmail.com",
"added_by": "javapedia.net",
"status": "pending"
},
{
"email": "test3#gmail.com",
"status": "pending"
}
]
}
]
Query1: Using elemMatch operator, mongodb playground: https://mongoplayground.net/p/4cNgWJse86W
db.collection.update({
_id: 259,
"members": {
"$elemMatch": {
"email": {
"$in": [
"test3#gmail.com",
"test4#gmail.com"
]
}
}
}
},
{
"$set": {
"members.$[].status": "active"
}
},
{
"multi": true
})
Query2: using $in, mongodb playground : https://mongoplayground.net/p/tNu395B2RFx
db.collection.update({
_id: 259,
"members.email": {
"$in": [
"test3#gmail.com",
"test4#gmail.com"
]
}
},
{
"$set": {
"members.$[].status": "active"
}
},
{
"multi": true
})
Expected result: only one element with test3#gmail.com status should be updated to active.
Actual result: both queries update all records.
Is this what you're looking for?
db.collection.update({
_id: 259,
},
{
"$set": {
"members.$[el].status": "active"
}
},
{
arrayFilters: [
{
"el.email": {
$in: [
"test3#gmail.com",
"test4#gmail.com"
]
}
}
]
})
You can put the initial conditions back if needed, I just keep this short (and to me they make no sense).
multi:true isn't needed for one document
Maybe better semantically to use updateOne()

mongodb - find previous and next document in aggregation framework

After applying a long pipeline to my collection I can obtain something like this:
{
{
"_id": "main1",
"title": "First",
"code": "C1",
"subDoc": {
"active": true,
"sub_id": "main1sub1",
"order": 1
}
},
{
"_id": "main2",
"title": "Second",
"code": "C2",
"subDoc": {
"active": true,
"sub_id": "main2sub1",
"order": 1
}
},
{
"_id": "main3",
"title": "Third",
"code": "C3",
"subDoc": {
"active": false,
"sub_id": "main3sub1",
"order": 1
}
}
}
The documents are already in the correct order. Now I have to find the document immediately preceding or following the one corresponding to a given parameter. For example, if I know { "code" : "C2" } I have to retrieve the previous document (example document with "code" : "C1").
I only need to get that document, not the others.
I know how to do it using the find () method and applying sort () and limit () in sequence, but I want to get the document directly in the aggregation pipeline, adding the necessary stages to do it.
I've tried some combinations of $ indexOfArray and $ arrayElemAt, but the first problem I encounter is that I don't have an array, it's just documents.
The second problem is that the parameter I know might sometimes be inside the subdocument, for example {"sub_id": "main3sub1"}, and again I should always get the previous or next parent document as a response (in the example, the pipeline should return document "main2" as previous document)
I inserted the collection in mongoplayground to be able to perform the tests quickly:
mongoplayground
Any idea?
If you want to retrieve only the previous document, use the following query:
First Approach:
Using $match,$sort,$limit
db.collection.aggregate([
{
$match: {
code: {
"$lt": "C2"
}
}
},
{
"$sort": {
code: -1
}
},
{
$limit: 1
}
])
MongoDB Playground
Second Approach:
As specified by # Wernfried Domscheit,
Converting to array and then using $arrayElemAt
db.collection.aggregate([
{
$group: {
_id: null,
data: {
$push: "$$ROOT"
}
}
},
{
$addFields: {
"index": {
$subtract: [
{
$indexOfArray: [
"$data.code",
"C2"
]
},
1
]
}
}
},
{
$project: {
_id: 0,
data: {
$arrayElemAt: [
"$data",
"$index"
]
}
}
},
{
$replaceRoot: {
newRoot: "$data"
}
}
])
MongoDB Playground

count based on nested key mongodb

How can i count based on xTag key is on doc
I tried this but it does not provide me actual count
db.collection.find({
"products.xTag": {
$exists: false
}
}).count();
when you run with $exist:true i would expect result 1
When you run with $exist:false i would expect result 3
Playground: https://mongoplayground.net/p/_gf7RzGc8oB
Structure:
[
{
"item": 1,
"products": [
{
"name": "xyz",
"xTag": 32423
},
{
"name": "abc"
}
]
},
{
"item": 2,
"products": [
{
"name": "bob",
},
{
"name": "foo"
}
]
}
]
It is not possible with find(), You can use aggregate(),
$unwind deconstruct products array
$match your condition
$count total documents
db.collection.aggregate([
{ $unwind: "$products" },
{ $match: { "products.xTag": { $exists: false } } },
{ $count: "count" }
])
Playground