I have case of collection that holds "item" data, with required node "owner" and optional node "status", e.g.
{ _id: 123, item: {some: "data 123" }, owner: {id: 456} }
{ _id: 124, item: {some: "data 124" }, owner: {id: 789}, status: { ok: 1} }
and when insert a new record, if the new owner.id equals the old owner.id, then i want the new record to have the same status
expressed in SQL it'd be something like
INSERT INTO mytable
(
id,
item_some,
owner_id,
status_ok
)
VALUES (
125,
'data 125',
789,
(SELECT COALESCE(status_ok, null) FROM mytable WHERE owner_id=789 LIMIT 1)
);
I'm looking at $expr, but not sure how to use it in db.collection.insert()...
Thanks in advance
I recommend you just execute a findOne first, like so:
const newObj = { item: {some: "data 123" }, owner: {id: 456} };
const prevObj = db.collection.findOne( {"owner._id": newObj.owner.id , status: {$exists: true}});
if (prevObj) {
newObj.status = prevObj.status
}
db.collection.insertOne(newObj)
This is essentially what your SQL query does, there is no way in Mongo to do this in just 1 call. (I will attach a "hacky" way to achieve this using the aggregation pipeline and the $merge stage however I do not recommend you use it as it has a lot of overhead and also requires at least 1 document in the collection )
db.collection.aggregate([
{
$facet: {
new: [
{
$limit: 1
},
{
$replaceRoot: {
newRoot: newObj
}
}
],
old: [
{
$match: {
"owner.id": newObj.owner.id,
status: {$exists: true}
}
}
]
},
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: [
{
$arrayElemAt: ["$new", 0]
},
{
$cond: [
{
$ne: [
{
$arrayElemAt: ["$old", 0]
},
null
]
},
{
status: {
$getField: {
field: "status",
input: {
$arrayElemAt: ["$old", 0]
}
}
}
},
{}
]
}
]
}
}
},
{
$merge: {
into: "collection",
whenNotMatched: "insert"
}
}
])
Mongo Playground
I have a document like this:
{
_id: 1,
data: [
{
_id: 2,
rows: [
{
myFormat: [1,2,3,4]
},
{
myFormat: [1,1,1,1]
}
]
},
{
_id: 3,
rows: [
{
myFormat: [1,2,7,8]
},
{
myFormat: [1,1,1,1]
}
]
}
]
},
I want to get distinct myFormat values as a complete array.
For example: I need the result as: [1,2,3,4], [1,1,1,1], [1,2,7,8]
How can I write mongoDB query for this?
Thanks for the help.
Please try this, if every object in rows has only one field myFormat :
db.getCollection('yourCollection').distinct('data.rows')
Ref : mongoDB Distinct Values for a field
Or if you need it in an array & also objects in rows have multiple other fields, try this :
db.yourCollection.aggregate([{$project :{'data.rows.myFormat':1}},{ $unwind: '$data' }, { $unwind: '$data.rows' },
{ $group: { _id: '$data.rows.myFormat' } },
{ $group: { _id: '', distinctValues: { $push: '$_id' } } },
{ $project: { distinctValues: 1, _id: 0 } }])
Or else:
db.yourCollection.aggregate([{ $project: { values: '$data.rows.myFormat' } }, { $unwind: '$values' }, { $unwind: '$values' },
{ $group: { _id: '', distinctValues: { $addToSet: '$values' } } }, { $project: { distinctValues: 1, _id: 0 } }])
Above aggregation queries would get what you wanted, but those can be tedious on large datasets, try to run those and check if there is any slowness, if you're using for one-time then if needed you can consider using {allowDiskUse: true} & irrespective of one-time or not you need to check on whether to use preserveNullAndEmptyArrays:true or not.
Ref : allowDiskUse , $unwind preserveNullAndEmptyArrays
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}
}
}
]
);
I have a collection named flights. In that collection I have two fields: origin_country and the dest_country.
These fields just keep record of the origin country and the destination country of a particular flight.
I'm trying to write a query which will return:
All theinternational flights only (i.e - where the origin and destination countries are different).
The sum of each (i.e - # of occurences of that flight in the collection).
The problem with the query which I have already is that it's also returning the domestic flights when ran:
QUERY:
db.flights.aggregate([
{$match: {
checking_countries_dont_match: { $ne: ["origin_country", "dest_country"] }
} },
{$group: {
_id: {
origin_country: "$origin_country",
dest_country: "$dest_country"
},
"count": {$sum:1}
} },
{$sort: {"count":-1} }
])
DOCUMENT SAMPLES:
{
"_id" : "2675594611",
"origin_country" : "Germany",
"dest_country" : "United Arab Emirates",
"airline_name" : "etihad-airways-etd"
}
{
"_id" : "2661517182",
"origin_country" : "Thailand",
"dest_country" : "Thailand",
"airline_name" : "nok-air-nok",
}
UPDATE
I changed the query to the following, but I still get results where the origin and destination are the same:
db.flights.aggregate([
{ $project: {
dont_match: { $ne: ["origin_country", "dest_country"] },
origin_country: "$origin_country",
dest_country: "$dest_country",
airline_name: "$airline_name"
} },
{ $match: {
airline_name: "etihad-airways-etd",
dont_match: true
} },
{ $group: {
id: {
origin_country: "$origin_country",
dest_country: "$dest_country"
}
} }
]);
UPDATE 2
Wokring Query:
db.flights.aggregate([
{ $project: {
dont_match: { $ne: ["$origin_country", "$dest_country"] },
origin_country: "$origin_country",
dest_country: "$dest_country",
airline_name: "$airline_name"
} },
{ $match: {
airline_name: "etihad-airways-etd",
dont_match: true
} },
{ $group: {
_id: {
origin_country: "$origin_country",
dest_country: "$dest_country"
},
count: {$sum:1}
} },
{ $sort: {count:-1}}
]);
Thanks for the help everyone :)
All you need to do is to modify your query a little bit. Look at this example, should give you an idea:
db.flights.aggregate([
{
$project: {
dont_match: { $ne: ["$origin_country", "$dest_country"] },
...
}
},
{
$match: {
dont_match: true
}
},
...
]);
Can you please try below query whether it match your expectation or not:
db.flights.aggregate([
{ $project: {
dont_match: {$cmp: ['$origin_country', '$dest_country']},
origin_country: "$origin_country",
dest_country: "$dest_country"
} },
{ $match: { dont_match: {$ne: 0} } }
]);
I have used aggregation for fetching records from mongodb.
$result = $collection->aggregate(array(
array('$match' => $document),
array('$group' => array('_id' => '$book_id', 'date' => array('$max' => '$book_viewed'), 'views' => array('$sum' => 1))),
array('$sort' => $sort),
array('$skip' => $skip),
array('$limit' => $limit),
));
If I execute this query without limit then 10 records will be fetched. But I want to keep limit as 2. So I would like to get the total records count. How can I do with aggregation? Please advice me. Thanks
Since v.3.4 (i think) MongoDB has now a new aggregation pipeline operator named 'facet' which in their own words:
Processes multiple aggregation pipelines within a single stage on the same set of input documents. Each sub-pipeline has its own field in the output document where its results are stored as an array of documents.
In this particular case, this means that one can do something like this:
$result = $collection->aggregate([
{ ...execute queries, group, sort... },
{ ...execute queries, group, sort... },
{ ...execute queries, group, sort... },
{
$facet: {
paginatedResults: [{ $skip: skipPage }, { $limit: perPage }],
totalCount: [
{
$count: 'count'
}
]
}
}
]);
The result will be (with for ex 100 total results):
[
{
"paginatedResults":[{...},{...},{...}, ...],
"totalCount":[{"count":100}]
}
]
This is one of the most commonly asked question to obtain the paginated result and the total number of results simultaneously in single query. I can't explain how I felt when I finally achieved it LOL.
$result = $collection->aggregate(array(
array('$match' => $document),
array('$group' => array('_id' => '$book_id', 'date' => array('$max' => '$book_viewed'), 'views' => array('$sum' => 1))),
array('$sort' => $sort),
// get total, AND preserve the results
array('$group' => array('_id' => null, 'total' => array( '$sum' => 1 ), 'results' => array( '$push' => '$$ROOT' ) ),
// apply limit and offset
array('$project' => array( 'total' => 1, 'results' => array( '$slice' => array( '$results', $skip, $length ) ) ) )
))
Result will look something like this:
[
{
"_id": null,
"total": ...,
"results": [
{...},
{...},
{...},
]
}
]
Use this to find total count in resulting collection.
db.collection.aggregate( [
{ $match : { score : { $gt : 70, $lte : 90 } } },
{ $group: { _id: null, count: { $sum: 1 } } }
] );
You can use toArray function and then get its length for total records count.
db.CollectionName.aggregate([....]).toArray().length
Here are some ways to get total records count while doing MongoDB Aggregation:
Using $count:
db.collection.aggregate([
// Other stages here
{ $count: "Total" }
])
For getting 1000 records this takes on average 2 ms and is the fastest way.
Using .toArray():
db.collection.aggregate([...]).toArray().length
For getting 1000 records this takes on average 18 ms.
Using .itcount():
db.collection.aggregate([...]).itcount()
For getting 1000 records this takes on average 14 ms.
Use the $count aggregation pipeline stage to get the total document count:
Query :
db.collection.aggregate(
[
{
$match: {
...
}
},
{
$group: {
...
}
},
{
$count: "totalCount"
}
]
)
Result:
{
"totalCount" : Number of records (some integer value)
}
I did it this way:
db.collection.aggregate([
{ $match : { score : { $gt : 70, $lte : 90 } } },
{ $group: { _id: null, count: { $sum: 1 } } }
] ).map(function(record, index){
print(index);
});
The aggregate will return the array so just loop it and get the final index .
And other way of doing it is:
var count = 0 ;
db.collection.aggregate([
{ $match : { score : { $gt : 70, $lte : 90 } } },
{ $group: { _id: null, count: { $sum: 1 } } }
] ).map(function(record, index){
count++
});
print(count);
//const total_count = await User.find(query).countDocuments();
//const users = await User.find(query).skip(+offset).limit(+limit).sort({[sort]: order}).select('-password');
const result = await User.aggregate([
{$match : query},
{$sort: {[sort]:order}},
{$project: {password: 0, avatarData: 0, tokens: 0}},
{$facet:{
users: [{ $skip: +offset }, { $limit: +limit}],
totalCount: [
{
$count: 'count'
}
]
}}
]);
console.log(JSON.stringify(result));
console.log(result[0]);
return res.status(200).json({users: result[0].users, total_count: result[0].totalCount[0].count});
Solution provided by #Divergent does work, but in my experience it is better to have 2 queries:
First for filtering and then grouping by ID to get number of filtered elements. Do not filter here, it is unnecessary.
Second query which filters, sorts and paginates.
Solution with pushing $$ROOT and using $slice runs into document memory limitation of 16MB for large collections. Also, for large collections two queries together seem to run faster than the one with $$ROOT pushing. You can run them in parallel as well, so you are limited only by the slower of the two queries (probably the one which sorts).
I have settled with this solution using 2 queries and aggregation framework (note - I use node.js in this example, but idea is the same):
var aggregation = [
{
// If you can match fields at the begining, match as many as early as possible.
$match: {...}
},
{
// Projection.
$project: {...}
},
{
// Some things you can match only after projection or grouping, so do it now.
$match: {...}
}
];
// Copy filtering elements from the pipeline - this is the same for both counting number of fileter elements and for pagination queries.
var aggregationPaginated = aggregation.slice(0);
// Count filtered elements.
aggregation.push(
{
$group: {
_id: null,
count: { $sum: 1 }
}
}
);
// Sort in pagination query.
aggregationPaginated.push(
{
$sort: sorting
}
);
// Paginate.
aggregationPaginated.push(
{
$limit: skip + length
},
{
$skip: skip
}
);
// I use mongoose.
// Get total count.
model.count(function(errCount, totalCount) {
// Count filtered.
model.aggregate(aggregation)
.allowDiskUse(true)
.exec(
function(errFind, documents) {
if (errFind) {
// Errors.
res.status(503);
return res.json({
'success': false,
'response': 'err_counting'
});
}
else {
// Number of filtered elements.
var numFiltered = documents[0].count;
// Filter, sort and pagiante.
model.request.aggregate(aggregationPaginated)
.allowDiskUse(true)
.exec(
function(errFindP, documentsP) {
if (errFindP) {
// Errors.
res.status(503);
return res.json({
'success': false,
'response': 'err_pagination'
});
}
else {
return res.json({
'success': true,
'recordsTotal': totalCount,
'recordsFiltered': numFiltered,
'response': documentsP
});
}
});
}
});
});
This could be work for multiple match conditions
const query = [
{
$facet: {
cancelled: [
{ $match: { orderStatus: 'Cancelled' } },
{ $count: 'cancelled' }
],
pending: [
{ $match: { orderStatus: 'Pending' } },
{ $count: 'pending' }
],
total: [
{ $match: { isActive: true } },
{ $count: 'total' }
]
}
},
{
$project: {
cancelled: { $arrayElemAt: ['$cancelled.cancelled', 0] },
pending: { $arrayElemAt: ['$pending.pending', 0] },
total: { $arrayElemAt: ['$total.total', 0] }
}
}
]
Order.aggregate(query, (error, findRes) => {})
I needed the absolute total count after applying the aggregation. This worked for me:
db.mycollection.aggregate([
{
$group: {
_id: { field1: "$field1", field2: "$field2" },
}
},
{
$group: {
_id: null, count: { $sum: 1 }
}
}
])
Result:
{
"_id" : null,
"count" : 57.0
}
If you don't want to group, then use the following method:
db.collection.aggregate( [
{ $match : { score : { $gt : 70, $lte : 90 } } },
{ $count: 'count' }
] );
Here is an example with Pagination, match and sort in mongoose aggregate
const [response] = await Prescribers.aggregate([
{ $match: searchObj },
{ $sort: sortObj },
{
$facet: {
response: [{ $skip: count * page }, { $limit: count }],
pagination: [
{
$count: 'totalDocs',
},
{
$addFields: {
page: page + 1,
totalPages: {
$floor: {
$divide: ['$totalDocs', count],
},
},
},
},
],
},
},
]);
Here count is the limit of each page and page is the the page number. Prescribers is the model
This would return the records similar to this
"data": {
"response": [
{
"_id": "6349308c90e58c6820bbc682",
"foo": "bar"
}
{
"_id": "6349308c90e58c6820bbc682",
"foo": "bar"
},
{
"_id": "6349308c90e58c6820bbc682",
"foo": "bar"
}
{
"_id": "6349308c90e58c6820bbc682",
"foo": "bar"
},
{
"_id": "6349308c90e58c6820bbc682",
"foo": "bar"
},
{
"_id": "6349308c90e58c6820bbc682",
"foo": "bar"
}
{
"_id": "6349308c90e58c6820bbc682",
"foo": "bar"
},
{
"_id": "6349308c90e58c6820bbc682",
"foo": "bar"
}
{
"_id": "6349308c90e58c6820bbc682",
"foo": "bar"
},
{
"_id": "6349308c90e58c6820bbc682",
"foo": "bar"
},
],
"pagination": [
{
"totalDocs": 592438,
"page": 1,
"totalPages": 59243
}
]
}
Sorry, but I think you need two queries. One for total views and another one for grouped records.
You can find useful this answer
if you need to $match with nested documents then
https://mongoplayground.net/p/DpX6cFhR_mm
db.collection.aggregate([
{
"$unwind": "$tags"
},
{
"$match": {
"$or": [
{
"tags.name": "Canada"
},
{
"tags.name": "ABC"
}
]
}
},
{
"$group": {
"_id": null,
"count": {
"$sum": 1
}
}
}
])
I had to perform a lookup, match and then count the documents recieved. Here is how I achieved it using mongoose:
ModelName.aggregate([
{
'$lookup': {
'from': 'categories',
'localField': 'category',
'foreignField': '_id',
'as': 'category'
}
}, {
'$unwind': {
'path': '$category'
}
}, {
'$match': {
'category.price': {
'$lte': 3,
'$gte': 0
}
}
}, {
'$count': 'count'
}
]);