I have collections in mongoDb like this:
{
_id: ObjectId(""),
fieldA: "123",
fieldB: {
subFieldB:[
{
fieldC: ""
},
{
fieldC: ""
},
{
fieldC: ""
}
]
}
},
{
_id: ObjectId(""),
fieldA: "123",
fieldB: {
subFieldB:[
{
fieldC: ""
},
{
fieldC: ""
},
{
fieldC: ""
}
]
}
},
{
_id: ObjectId(""),
fieldA: "456",
fieldB: {
subFieldB:[
{
fieldC: ""
},
{
fieldC: ""
},
{
fieldC: ""
}
]
}
}
I want to get sum of number of subFieldB where fieldA is "123"
So the goal is getting something like {sumOfSubFieldB: 6}
I tried aggregate like:
db.collection.aggregate([
{
$match : { "fieldA" : "123" }
},
{
$project: {
numberOfFieldB: { $cond: { if: { $isArray: "$fieldB.subFieldB" }, then: { $size: "$fieldB.subFieldB" }, else: "NA"} },
}
}
] )
So far I get sum of fieldB per doc like:
{
"_id" : ObjectId(""),
"numberOfFieldB" : 3
}
{
"_id" : ObjectId(""),
"numberOfFieldB" : 3
}
How should I do the query to get total number of fieldB?
Thank you
Try this one, or something similar with $group:
db.collection.aggregate(
[{
$match: {
"fieldA": "123"
}
}, {
$group: {
_id: "$fieldA",
count: {
$sum: { $size: "$fieldB.subFieldB" } }
}
}]
);
Related
I have a collection of documents that look like this
{
_id : 21353456,
product : "xy",
text : "asdf",
reviews : [
{
username : "User1",
userID: 12
text : "hi"
rate: 4,
},
{
username : "User2",
userID: 123
text : "hi1"
rate:2,
}
]
}
I want to retrieve the average rating for user1 on all the product they have rated.
db.collection.aggregate([{$unwind: "$reviews"},{$match: {"$review.userID": "12"}},{$group: { _id: "$reviews.userName",
{avgRate: {$avg: "$reviews.rate"}}})]
)
I tried this but I keep getting unexpected token errors for "," where the last ")" is.
You did this:
db.collection.aggregate([
{ $unwind: "$reviews" },
{ $match: { "$review.userID": "12" } },
{ $group: { _id: "$reviews.userName",
{ avgRate: { $avg: "$reviews.rate" } }})]
)
Must be this:
db.collection.aggregate([
{ $unwind: "$reviews" },
{ $match: { "$review.userID": "12" } },
{
$group: {
_id: "$reviews.userName",
avgRate: { $avg: "$reviews.rate" }
}
}
])
MongoDB version:4.2.17.
Trying out aggregation on data in a collection.
Example data:
{
"_id" : "244",
"pubName" : "p1",
"serviceIdRef" : "36e9c779-7865-4b74-a30b-e4d6a0cc5295",
"serviceName" : "my-service",
"subName" : "c1",
"pubState" : "INVITED"
}
I would like to:
Do a match by something (let’s say subName) and group by serviceIdRef and then limit to return X entries
Also return for each of the serviceIdRefs, the count of the documents in each of ACTIVE or INVITED states. And Y (for this example, say Y=3) documents that are in this state.
For example, the output would appear as (in brief):
[
{
serviceIdRef: "36e9c779-7865-4b74-a30b-e4d6a0cc5295",
serviceName:
state:[
{
pubState: "INVITED"
count: 200
sample: [ // Get those Y entries (here Y=3)
{
// sample1 like:
"_id" : "244",
"pubName" : "p1",
"serviceIdRef" : "36e9c779-7865-4b74-a30b-e4d6a0cc5295",
"serviceName" : "my-service",
"subName" : "c1",
"pubState" : "INVITED"
},
{
sample2
},
{
sample3
}
]
},
{
pubState: "ACTIVE", // For this state, repeat as we did for "INVITED" state above.
......
}
]
}
{
repeat for another service
}
]
So far I have written this but am not able to get those Y entries. Is there a (better) way?
This is what I have so far (not complete and not exactly outputs in the format above):
db.sub.aggregate(
[{
$match:
{
"subName": {
$in: ["c1", "c2"]
},
"$or": [
{
"pubState": "INVITED",
},
{
"pubState": "ACTIVE",
}
]
}
},
{
$group: {
_id: "$serviceIdRef",
subs: {
$push: "$$ROOT",
}
}
},
{
$sort: {
_id: -1,
}
},
{
$limit: 22
},
{
$facet:
{
facet1: [
{
$unwind: "$subs",
},
{
$group:
{
_id: {
"serviceName" : "$_id",
"pubState": "$subs.pubState",
"subState": "$subs.subsState"
},
count: {
$sum: 1
}
}
}
]
}
}
])
You have to do the second $group stage to manage nested structure,
$match your conditions
$sort by _id in descending order
$group by serviceIdRef and pubState, get first required fields and prepare the array for sample, and get count of documents
$group by only serviceIdRef and construct the state array
$slice for limit the document in sample
db.collection.aggregate([
{
$match: {
subName: { $in: ["c1", "c2"] },
pubState: { $in: ["INVITED", "ACTIVE"] }
}
},
{ $sort: { _id: -1 } },
{
$group: {
_id: {
serviceIdRef: "$serviceIdRef",
pubState: "$pubState"
},
serviceName: { $first: "$serviceName" },
sample: { $push: "$$ROOT" },
count: { $sum: 1 }
}
},
{
$group: {
_id: "$_id.serviceIdRef",
serviceName: { $first: "$serviceName" },
state: {
$push: {
pubState: "$_id.pubState",
count: "$count",
sample: { $slice: ["$sample", 22] }
}
}
}
}
])
Playground
db.customerOrder.insert({
firstName: "Andrew",
lastName: "Lee",
DOB: ISODate("1974-10-28T00:00:00Z"),
phone: "+1 (959) 567-3312",
email: "mark#gmail.com",
address: {
street: "Cornish Street, Victoria",
houseNumber: "68",
postalCode: "3024",
country: "Australia",
},
language: ["English", "Mandarin"],
balance: 0,
orders: [
{
orderNumber: "ord003",
orderDate: ISODate("2020-01-10T00:00:00Z"),
staffNumber: "stf789"
}
]
});
Given the document above, and other documents which contain other orders and order number, how do i specify an aggregation so that it will only list all orderNumbers that's handled by a staffNumber x?
Example, orderNumber ord004 and ord005 is handled by staffNumber stf890
I tried doing
db.customerOrder.aggregate([ {"$match":{"orders.staffNumber":"stf890"}}, {"$project":{"orders.orderNumber":1, "_id":0}} ])
but the result was
{
"orders" : [
{
"orderNumber" : "ord003"
},
{
"orderNumber" : "ord003"
},
{
"orderNumber" : "ord005"
}
]
}
{
"orders" : [
{
"orderNumber" : "ord001"
},
{
"orderNumber" : "ord005"
}
]
}
{
"orders" : [
{
"orderNumber" : "ord003"
},
{
"orderNumber" : "ord004"
}
]
}
I expect the result to output only ord004 and ord005
How do i achieve this?
Thank you for your help
Try this! your query is almost correct but you're missing the case of matching orderNumber.
db.customerOrder.aggregate([
{
"$match":{
"orders.staffNumber":"stf890"
}
},
{
$unwind:{
"path":"$orders"
}
},
{
"$match":{
"orders.orderNumber":{$in:["ord004","ord005"]}
}
},
{
"$project":{
"orders.orderNumber":1,
"_id":0
}
}
])
If you don't care about the structure you can just $unwind and then match. otherwise you need to use something like $filter
Option 1:
db.customerOrder.aggregate([
{
"$match": {
"orders.staffNumber": "stf890"
}
},
{
"$unwind": "$orders"
},
{
"$match": {
"orders.staffNumber": "stf890"
}
},
{
"$project": {"orders.orderNumber": 1, "_id": 0}
}])
Option 2:
db.customerOrder.aggregate([
{
"$match": {
"orders.staffNumber": "stf890"
}
},
{
$project: {
orders: {
$filter: {
input: "$orders",
as: "order",
cond: {
$eq: ["$$order.staffNumber", "stf890"]
}
}
}
}
},
{
"$project": {
"orders.orderNumber": 1,
"_id": 0
}
}
])
Need help with some MongoDB query:
The document I have is below and I am trying to search based on 2 conditions
The meta.tags.code = "ABC"
Its LastSyncDateTime should
meta.extension.value == "" (OR)
the meta.extension.value is less than meta.lastUpdated
Data :
{
"meta" : {
"extension" : [
{
"url" : "LastSyncDateTime",
"value" : "20190206-00:49:25.694"
},
{
"url" : "RetryCount",
"value" : "0"
}
],
"lastUpdate" : "20190207-01:21:41.095",
"tags" : [
{
"code" : "ABC",
"system" : "type"
},
{
"code" : "XYZ",
"system" : "SourceSystem"
}
]
}
}
Query:
db.proc_patients_service.find({
"meta.tags.code": "ABC",
$or: [{
"meta.extension.value": ""
}, {
$expr: { "$lt": [{ "mgfunc": "ISODate", "params": [{ "$arrayElemAt": ["$meta.extension.value", 0] }] }, { "mgfunc": "ISODate", "params": ["$meta.lastUpdate"] }] }
}]
})
But it is only fetching ABC Patients whose LastSyncDateTime is empty and ignores the other condition.
Using MongoDB Aggregation, I have converted your string to date with operator $dateFromString and then compare the value as per your criteria.
db.proc_patients_service.aggregate([
{ $match: { "meta.tags.code": "ABC", } },
{ $unwind: "$meta.extension" },
{
$project: {
'meta.tags': '$meta.tags',
'meta.lastUpdate': { '$dateFromString': { 'dateString': '$meta.lastUpdate', format: "%Y%m%d-%H:%M:%S.%L" } },
'meta.extension.url': '$meta.extension.url',
'meta.extension.value': {
$cond: {
if: { $ne: ["$meta.extension.value", "0"] }, then: { '$dateFromString': { 'dateString': '$meta.extension.value', format: "%Y%m%d-%H:%M:%S.%L" } }, else: 0
}
}
}
},
{
$match: {
$or: [
{ "meta.extension.value": 0 },
{ $expr: { $lt: ["$meta.extension.value", "$meta.lastUpdate"] } }
]
}
},
{
$group: { _id: '_id', 'extension': { $push: '$meta.extension' }, "lastUpdate": { $first: '$meta.lastUpdate' }, 'tags': { $first: '$meta.tags' } }
},
{
$project: { meta: { 'extension': '$extension', lastUpdate: '$lastUpdate', 'tags': '$tags' } }
}
])
I am trying to fetch all records (and count of all records) for a structure like the following,
{
id: 1,
level1: {
level2:
[
{
field1:value1;
},
{
field1:value1;
},
]
}
},
{
id: 2,
level1: {
level2:
[
{
field1:null;
},
{
field1:value1;
},
]
}
}
My requirement is to fetch the number of records that have field1 populated (atleast one in level2). I need to say fetch all the ids or the number of such ids.
The query I am using is,
db.table.find({},
{
_id = id,
value: {
$elemMatch: {'level1.level2.field1':{$exists: true}}
}
}
})
Please suggest.
EDIT1:
This is the question I was trying to ask in the comment. I was unable to elucidate in the comment properly. Hence, editing the question.
{
id: 1,
level1: {
level2:
[
{
field1:value1;
},
{
field1:value1;
},
]
}
},
{
id: 2,
level1: {
level2:
[
{
field1:value2;
},
{
field1:value2;
},
{
field1:value2;
}
]
}
}
{
id: 3,
level1: {
level2:
[
{
field1:value1;
},
{
field1:value1;
},
]
}
}
The query we used results in
value1: 4
value2: 3
I want something like
value1: 2 // Once each for documents 1 & 3
value2: 1 // Once for document 2
You can do that with the following find query:
db.table.find({ "level1.level2" : { $elemMatch: { field1 : {$exists: true} } } }, {})
This will return all documents that have a field1 in the "level1.level2" structure.
For your question in the comment, you can use the following aggregation to "I had to return a grouping (and the corresponding count) for the values in field1":
db.table.aggregate(
[
{
$unwind: "$level1.level2"
},
{
$match: { "level1.level2.field1" : { $exists: true } }
},
{
$group: {
_id : "$level1.level2.field1",
count : {$sum : 1}
}
}
]
UPDATE: For your question "'value1 - 2` At level2, for a document, assume all values will be the same for field1.".
I hope i understand your question correctly, instead of grouping only on the value of field1, i added the document _id as an xtra grouping:
db.table.aggregate(
[
{
$unwind: "$level1.level2"
},
{
$match: {
"level1.level2.field1" : { $exists: true }
}
},
{
$group: {
_id : { id : "$_id", field1: "$level1.level2.field1" },
count : {$sum : 1}
}
}
]
);
UPDATE2:
I altered the aggregation and added a extra grouping, the aggregation below gives you the results you want.
db.table.aggregate(
[
{
$unwind: "$level1.level2"
},
{
$match: {
"level1.level2.field1" : { $exists: true }
}
},
{
$group: {
_id : { id : "$_id", field1: "$level1.level2.field1" }
}
},
{
$group: {
_id : { id : "$_id.field1"},
count : { $sum : 1}
}
}
]
);