I'm trying to find specific element of array in my MongoDB document, but with all my tries query returns all array. How can I get the right object?
My collection's document looks like:
{
"Achievements": [
{
"AchievementID": 1,
"AchievementEventID": 0
},
{
"AchievementID": 2,
"AchievementEventID": 1
}
],
"Buildings": [
{
"BuildingID": 1,
"BuildingType": "type1"
},
{
"BuildingID": 2,
"BuildingType": "type1"
},
]
}
I tried to get only one element of my Achievements array:
db.data.find({'Achievements.AchievementEventID': 0})
I expected to get only element with AchievementEventID equal to 0:
{
"AchievementID": 1,
"AchievementEventID": 0
}
But I got whole Achievements array.
How I could get only specific element?
You can use the Aggregation query like in the following example. The sample achive collection has 3 documents:
{
"_id" : 1,
"Achievements" : [
{
"AchievementID" : 1,
"AchievementEventID" : 0
},
{
"AchievementID" : 2,
"AchievementEventID" : 1
}
],
"Buildings" : [
{
"BuildingID" : 1,
"BuildingType" : "type1"
},
{
"BuildingID" : 2,
"BuildingType" : "type1"
}
]
}
{
"_id" : 2,
"Achievements" : [
{
"AchievementID" : 2,
"AchievementEventID" : 2
}
],
"Buildings" : [
{
"BuildingID" : 2,
"BuildingType" : "type2"
}
]
}
{
"_id" : 3,
"Achievements" : [
{
"AchievementID" : 31,
"AchievementEventID" : 1
}
],
"Buildings" : [
{
"BuildingID" : 3,
"BuildingType" : "type3"
}
]
}
The Query:
db.achive.aggregate( [
{ $project: { Buildings: 0} },
{ $unwind: "$Achievements" },
{ $match: { "Achievements.AchievementEventID": { $eq: 1 } } }
])
=>
{ "_id" : 1, "Achievements" : { "AchievementID" : 2, "AchievementEventID" : 1 } }
{ "_id" : 3, "Achievements" : { "AchievementID" : 31, "AchievementEventID" : 1 } }
I added the _id field to identify the documents selected.
Updated Query:
db.achive.aggregate( [
{ $unwind: "$Achievements" },
{ $match: { "Achievements.AchievementEventID": { $eq: 1 } } },
{ $project: { AchievementID: "$Achievements.AchievementID", _id: 0} },
])
=>
{ "AchievementID" : 2 }
{ "AchievementID" : 31 }
You can use $elemMatch for that
db.data.find(
{ 'Achievements.AchievementEventID': 0},{Buildings:0, Achievements: {$elemMatch: {AchievementEventID: 0}}}
)
If the above solution doesn't for you then try the below one:
db.data.find({"Achievements.AchievementEventID": 0}, {Buildings: 0, 'Achievements.$': 1});
You can use aggregate instead of a find like this
db.data.aggregate(
[{'$unwind':'$Achievements'},{$match:{
'Achievements.AchievementEventID':0
}},{$project:{
'AchievementID':'$Achievements.AchievementID',
'AchievementEventID':'$Achievements.AchievementEventID'
}}]
);
Related
Is there a way to get only specific field values from a multi array?
db.test.insert({'id':'stack', 'nums':[{'name':'Dave', 'num': 1, 'phones' :[{'id':1, 'number':'000-000'}, {'id':2, 'number':'000-001'}]}]})
{
"_id" : "63b3896a5b5d9de96b6277fa",
"id" : "stack",
"nums" : [
{
"name" : "Dave",
"num" : 1,
"phones" : [
{
"id" : 1,
"number" : 0
},
{
"id" : 2,
"number" : -1
}
]
}
]
}
the answer i want
{'id':1, 'number':'000-000'}
I want to print only the value with 'id' 1 in 'phones'.
db.collection.aggregate([
{
"$unwind": "$nums"
},
{
$project: {
"phones": {
$filter: {
input: "$nums.phones",
cond: {$eq: ["$$this.id",1]
}
}
}
}
}
])
Playground
I'm encountering a strange behavior in MongoDB.
This is the schema of the documents in my collection:
{
"_id" : ObjectId("62bf10951fecaed4dba275b1"),
"name" : "Rack 1",
"type" : "rack",
"positions" : [
{
"number" : 1
},
{
"number" : 2
},
{
"number" : 3,
"nodes" : [
{
"number" : 1
}
]
},
{
"number" : 4,
"nodes" : [
{
"number" : 1
},
{
"number" : 2
},
{
"number" : 3
}
]
},
]
}
I would like to run two queries:
create a new position, if it doesn't exist yet
create a new object in the nodes array
I have created two queries so far:
db.getCollection('locations').updateOne(
{ _id: ObjectId("62bf10951fecaed4dba275b1") },
{ $addToSet: { 'positions': { number: 5 } } }
)
db.getCollection('locations').updateOne(
{ _id: ObjectId("62bf10951fecaed4dba275b1"), 'positions.number': 5 },
{ $addToSet: { 'positions.$.nodes': { number: 1 } } }
)
The second query creates a new element in nodes. BUT it also creates a new element in the positions array.
This is the erroneous object:
{
"_id" : ObjectId("62bf10951fecaed4dba275b1"),
"name" : "Rack 1",
"type" : "rack",
"positions" : [
{
"number" : 1
},
{
"number" : 2
},
{
"number" : 3,
"nodes" : [
{
"number" : 1
}
]
},
{
"number" : 4,
"nodes" : [
{
"number" : 1
},
{
"number" : 2
},
{
"number" : 3
}
]
},
{
"number" : 5,
"nodes" : [
{
"number" : 1
}
]
},
{
"number" : 5 <<<<<< THIS IS WRONG
},
]
}
Is there something I need to know or is simply the wrong query?
Many thanks
The two queries may run at different lengths of time and should not be executed synchronously.
Please try using some async/await style ordering to ensure that the second query does indeed execute when the first query is finished.
e.g. in javascript:
const myFunction = async () => {
await db.getCollection('locations').updateOne(
{ _id: ObjectId("62bf10951fecaed4dba275b1") },
{ $addToSet: { 'positions': { number: 5 } } }
)
await db.getCollection('locations').updateOne(
{ _id: ObjectId("62bf10951fecaed4dba275b1"), 'positions.number': 5 },
{ $addToSet: { 'positions.$.nodes': { number: 1 } } }
)
}
myFunction()
I have data in mongodb with multiple fields, I am trying to filter data on basis of a field named create_date and then trying to fetch totalrecordscount along with further filtering the data. Following is the data structure:
"_id" : ObjectId("62a886a76034628f8028e8dc"),
"create_time" : "18:53:01",
"close_date" : "2022-05-09",
"close_time" : "13:34:43",
"country_code" : "US",
"closed_case" : 1,
"resolution_days" : 8,
"status_code" : "5",
"state_code" : "1",
"issue_resolved_flag" : "Yes",
"incident_created_by" : "09D4A6BB-C51E-EB11-A813-000D3A58F938",
"incident_modified_by" : "A3CBC776-DF3C-E711-810B-E0071B7284D1",
"modifiedon" : "2022-05-09 13:34:46.0",
"row_insertion_dttm" : "2022-06-14 02:58:21.202",
"data_source_category" : "CASE",
"resolution_duration_minutes" : 5060,
"create_date" : "2022-05-01",
"repeat_case_different_issue7_day" : 0,
"repeat_case_same_issue_7day" : 0,
"scr_7day" : 1,
"ocr_7day" : 0,
"csat_status" : "no"
I am able to aggregate the data on basis of create date and fetch the totalrecordscount for a particular date using following command :
country_code:1
}},
{
$match:{create_date:{$gt:"2022-06-01"}}
},
{$group:{ _id: {datebasis: "$create_date"},
TotalRecordscount: { $sum: 1 },
}
},
])
The output is: {
"_id" : {
"datebasis" : "2022-06-17"
},
"TotalRecordscount" : 13254.0
}
/* 2 */
{
"_id" : {
"datebasis" : "2022-06-14"
},
"TotalRecordscount" : 16688.0
}
/* 3 */
{
"_id" : {
"datebasis" : "2022-06-09"
},
"TotalRecordscount" : 15478.0
}
But my ask is to further group the data to get the number of records on a particular date for fields like "scr_7day" equals to 0 or "resolution_duration_minutes" < 1440.
Can you help me in achieving this?
Assume you solve the date string logic as mentioned in the comment, my answer just focuses on your question.
You can work with $count and $cond operators to calculate the documents by condition.
db.collection.aggregate([
{
$group: {
_id: {
datebasis: "$create_date"
},
TotalRecordscount: {
$sum: 1
},
scr_7dayIsZero: {
$sum: {
$cond: {
if: {
$eq: [
"$scr_7day",
0
]
},
then: 1,
else: 0
}
}
},
resolution_duration_minutesLessThan1440: {
$sum: {
$cond: {
if: {
$lt: [
"$resolution_duration_minutes",
1440
]
},
then: 1,
else: 0
}
}
}
}
}
])
Sample Mongo Playground
I have a MongoDB collection called books.
An example of a document is:
{
"_id" : ObjectId("62bf10951fecaed4dba275b1"),
"name" : "Library 1",
"positions" : [
{
"number" : 2,
"nodes" : [
{
"number" : 2,
"bookId" : "6254674d3711f90bd8e76036"
},
{
"number" : 1,
"bookId" : "621e9b5aa7951d0be4516c18"
}
]
},
{
"number" : 1,
"nodes" : [
{
"number" : 1,
"bookId" : "6254674d3711f90bd8e76037"
},
{
"number" : 3,
"bookId" : "6254674d3711f90bd8e76039"
},
{
"number" : 2,
"bookId" : "6254674d3711f90bd8e76035"
}
]
}
]
}
I need to run a query that, based on a book ID, returns the name, the positions.number and the positions.node.number.
For example, if I search for the ID 6254674d3711f90bd8e76035, it should return:
{
_id: ObjectId("62bf10951fecaed4dba275b1"),
name: "Library 1",
positions: {
number: 1,
nodes: {
number: 2
}
}
}
So far, this is what I came out with:
db.getCollection('books').aggregate([
{ $match: { "positions.nodes.bookId": "6254674d3711f90bd8e76035" } },
{ $project: { name: 1, "positions.number": 1, "positions.nodes.number": 1 } }
])
Unfortunately, this returns every single node. I might need something that says:
"Select name, position.number, position.nodes.number where bookId = 6254674d3711f90bd8e76035"
Any help is appreciated.
Thanks
Solved:
db.getCollection('books').aggregate([
{
$match: {"positions.nodes.books": "6254674d3711f90bd8e76035"}
},
{
$unwind: "$positions"
},
{
$unwind: "$positions.nodes"
},
{
$match: {"positions.nodes.books": "6254674d3711f90bd8e76035"}
},
])
I would like to either insert a new document with a default value as part of an array, or update that part of the array if the document already exists.
What I thought of was:
db.test.update(
{ "a": 5 },
{ $setOnInsert: { "b": [0, 0] }, $min: { "b.0": 5 } },
{ upsert: true }
);
If I do that, then I get:
Cannot update 'b' and 'b.0' at the same time
Another idea was to remove $setOnInsert and just keep $min, since the minimum between nothing and 5 should be 5.
db.test.update(
{ "a": 5 },
{ $min: { "b.0": 5 } },
{ upsert: true }
);
This doesn't raise an error, but now the document I get is:
{ "a" : 5, "b" : { "0" : 5 } }
I need an array with 5 at position 0 however, not an object with a 0 property.
How can I achieve this?
You can use .bulkWrite() for this, and it's actually a prime use case of why this exists. It only sends "one" actual request to the server and has only one response. It's still two operations, but they are more or less tied together and generally atomic anyway:
db.junk.bulkWrite([
{ "updateOne": {
"filter": { "a": 1 },
"update": { "$setOnInsert": { "b": [ 5,0 ] } },
"upsert": true
}},
{ "updateOne": {
"filter": { "a": 1 },
"update": { "$min": { "b.0": 5 } }
}}
])
Run for the first time will give you an "upsert", note that it's "inserted" and not "modified" in the response:
{
"acknowledged" : true,
"deletedCount" : 0,
"insertedCount" : 0,
"matchedCount" : 1,
"upsertedCount" : 1,
"insertedIds" : {
},
"upsertedIds" : {
"0" : ObjectId("5947c412d6eb0b7d6ac37f09")
}
}
And the document of course looks like:
{
"_id" : ObjectId("5947c412d6eb0b7d6ac37f09"),
"a" : 1,
"b" : [
5,
0
]
}
Then run with a different value to $min as you likely would in real cases:
db.junk.bulkWrite([
{ "updateOne": {
"filter": { "a": 1 },
"update": { "$setOnInsert": { "b": [ 5,0 ] } },
"upsert": true
}},
{ "updateOne": {
"filter": { "a": 1 },
"update": { "$min": { "b.0": 3 } }
}}
])
And the response:
{
"acknowledged" : true,
"deletedCount" : 0,
"insertedCount" : 0,
"matchedCount" : 2,
"upsertedCount" : 0,
"insertedIds" : {
},
"upsertedIds" : {
}
}
Which "matched" 2 but of course $setOnInsert does not apply, so the result is:
{
"_id" : ObjectId("5947c412d6eb0b7d6ac37f09"),
"a" : 1,
"b" : [
3,
0
]
}
Just like it should be