Due to some changes to the schema, I've had a to do refactoring that's broken what was a simple filter in an application, in this instance is isToRead while everything else continues to work.
The document in "Assets" that should be appearing is:
{
"_id": {
"$oid": "ID"
},
"userId": "ID",
"folderId": "ID",
"title": "Title",
"note": "<p><strong>Note.</strong></p>",
"typeOfAsset": "web",
"isFavourite": false,
"createdAt": {
"$date": {
"$numberLong": "1666702053399"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1666702117855"
}
},
"isActive": 3,
"tags": [],
"traits": [
{
"$oid": "6357dae53298948a18a17c60"
}
]
"__v": 0
}
… and the reference document in "Assets_Traits" that I'm attempting to filter against should be:
{
"_id": {
"$oid": "6357dae53298948a18a17c60"
},
"userId": "ID",
"numberOfViews": 1,
"isToRead": true,
"__v": 0
}
I'll share the entire method, which includes the various attempts that — for whatever reason — won't work.
let tags = args.tags ? args.tags.split(',') : []
let tagsToMatch = []
if (tags.length > 0) {
tags.forEach(tag => {
tagsToMatch.push(new mongoose.Types.ObjectId(tag))
})
}
let search = {
...(args.phraseToSearch.length > 0 && {
$search: {
index: 'assets',
compound: {
must: [{
phrase: {
query: args.phraseToSearch,
path: 'title',
slop: 2,
score: { boost: { value: 3 } }
}
}],
should: [{
phrase: {
query: args.phraseToSearch,
path: 'note',
slop: 2
}
}]
}
}
})
}
let project = {
$project: {
_id: 0,
id: '$_id',
userId: 1,
folderId: 1,
title: 1,
note: 1,
typeOfAsset: 1,
isFavourite: 1,
createdAt: 1,
updatedAt: 1,
isActive: 1,
attributes: 1,
preferences: 1,
// ...(args.typeOfAttribute === 'isToRead' && {
// traits: {
// $filter: {
// input: "$traits",
// cond: { $eq: [ "$$this.isToRead", true ] }
// }
// }
// }),
tags: 1,
traits: 1,
score: {
$meta: 'searchScore'
}
}
}
let match = {
$match: {
userId: args.userId,
typeOfAsset: {
$in: args.typeOfAsset === 'all' ? MixinAssets().all : [args.typeOfAsset] // [ 'file', 'folder', 'message', 'note', 'social', 'web' ]
},
...(tagsToMatch.length > 0 && {
tags: {
$in: tagsToMatch
}
}),
...(args.typeOfAttribute === 'isToRead' && {
// $expr: {
// $allElementsTrue: [{
// $map: {
// input: '$traits',
// as: 't',
// in: {
// $and: [
// { $eq: [ "$$t.userId", args.userId ] },
// { $eq: [ "$$t.isToRead", true ] }
// ]
// }
// }
// }]
// }
// $expr: {
// $filter: {
// input: "$traits",
// cond: {
// $and: [
// { $eq: [ "$$this.userId", args.userId ] },
// { $eq: [ "$$this.isToRead", true ] }
// ]
// }
// }
// }
}),
isActive: 3
}
}
let lookup = {}
switch (args.typeOfAttribute) {
case 'areFavourites':
match.$match.isFavourite = true
break
case 'isToRead':
// match.$match.traits = {
// userId: args.userId,
// isToRead: true
// }
// match.$match.traits = {
// $elemMatch: {
// userId: args.userId,
// isToRead: true
// }
// }
// lookup = {
// $lookup: {
// from: 'assets_traits',
// let: { isToRead: '$isToRead' },
// pipeline: [{
// $match: {
// $expr: {
// $eq: [ '$isToRead', true ]
// }
// },
// }],
// localField: 'userId',
// foreignField: 'userId',
// as: 'traits'
// }
// }
break
case 'inTrash':
match.$match.isActive = 2
break
}
let skip = {
$skip: args.skip
}
let limit = {
$limit: args.first
}
let group = {
$group: {
_id: null,
count: { $sum: 1 }
}
}
let sort = {
$sort: {
[args.orderBy]: args.orderDirection === 'asc' ? 1 : -1
}
}
console.info('Queries:getAllAssetsForNarrative()', match.$match)
let allAssets = await Models.Assets.schema.aggregate(
(search.hasOwnProperty('$search')) // Order of criteria is critical to the functioning of the aggregate method.
? [search, project, match, sort, skip, limit]
: [match, project, sort, skip, limit]
// : [match, project, { $unwind: '$traits' }, { $match: { traits: { $elemMatch: { isToRead: true } } } }, sort, skip, limit]
)
let [ totalNumberOfAssets ] = await Models.Assets.schema.aggregate(
(search.hasOwnProperty('$search')) // Order of criteria is critical to the functioning of the aggregate method.
? [search, project, match, group]
: [match, project, group]
// : [match, project, { $unwind: '$traits' }, { $match: { traits: { $elemMatch: { isToRead: true } } } }, group]
)
await (() => {
if (args.phraseToSearch.length > 0) {
const SearchFactory = require('../services/search/search')
const Search = SearchFactory(Models)
Search.insertRecentSearch({
userId: args.userId,
phraseToSearch: args.phraseToSearch.toLowerCase()
})
}
})()
I removed lookup in the final two arrays for the aggregate function because it was becoming too complicated for to me understand what was happening.
Weird thing is, "Tags" match and it's also a reference, while "Assets_Traits" won't return or do anything.
The values for typeOfAsset are: [ 'file', 'folder', 'message', 'note', 'social', 'web' ]
While 'All Assets' is chosen, choosing 'To Read' performs a filter against all types of Assets, and additional filtering would happen when a specific type of Asset is chosen — as explained, this worked before the changes to the schema.
Also, ignore tags because those aren't in use here.
Thoughts appreciated!
You did not provide sample of your input (args) or the constants you use (for example MixinAssets().all which i'm suspecting is problematic).
I constructed my own input for the sake of this answer:
const args = {
typeOfAsset: 'isToRead',
typeOfAttribute: "isToRead",
tagsToMatch: ["tag1", "tag2"],
skip: 0,
first: 1,
orderBy: "_id",
orderDirection: "desc"
}
This produces the following pipeline (using your code):
db.Assets.aggregate([
{
"$match": {
"userId": "123",
"typeOfAsset": {
"$in": [
"isToRead"
]
},
"tags": {
"$in": [
"tag1",
"tag2"
]
},
"isActive": 3
}
},
{
"$project": {
"_id": 0,
"id": "$_id",
"userId": 1,
"folderId": 1,
"title": 1,
"note": 1,
"typeOfAsset": 1,
"isFavourite": 1,
"createdAt": 1,
"updatedAt": 1,
"isActive": 1,
"attributes": 1,
"preferences": 1,
"tags": 1,
"traits": 1,
"score": {
"$meta": "searchScore"
}
}
},
{
"$sort": {
"_id": -1
}
},
{
"$skip": 0
},
{
"$limit": 1
}
])
Which works, as you can see in this Mongo Playground sample.
So what is your issue? As I mentioned I suspect one issue is the MixinAssets().all if args.typeOfAsset === 'all' then you use that value, now if it's an array the match condition looks like this:
typeOfAsset: {
$in: [['web', '...', '...']]
}
This won't match anything as it's an array of arrays, if it's a constant value then again it won't match as the type in the db is different.
I will give one more tip, usually when you want to build a pagination system like this and need both the results and totalResultCount it's common practice to use $facet this way you don't have to execute the pipeline twice and you can improve perfomance, like so:
db.Assets.aggregate([
{
"$match": {
"userId": "123",
"typeOfAsset": {
"$in": [
"isToRead"
]
},
"tags": {
"$in": [
"tag1",
"tag2"
]
},
"isActive": 3
}
},
{
$facet: {
totalCount: [
{
$group: {
_id: null,
count: {
$sum: 1
}
}
}
],
results: [
{
"$project": {
"_id": 0,
"id": "$_id",
"userId": 1,
"folderId": 1,
"title": 1,
"note": 1,
"typeOfAsset": 1,
"isFavourite": 1,
"createdAt": 1,
"updatedAt": 1,
"isActive": 1,
"attributes": 1,
"preferences": 1,
"tags": 1,
"traits": 1,
"score": {
"$meta": "searchScore"
}
}
},
{
"$sort": {
"_id": -1
}
},
{
"$skip": 0
},
{
"$limit": 1
}
]
}
}
])
Mongo Playground
await products.updateOne(
{
$and: [
{ name: { $eq: name } },
{ $expr: { $lt: ["$remaining", "$capacity"] } },
],
},
{ $inc: { remaining: 1 } },
{ returnOriginal: false }
);
Instead of having the condition in the query like so { $expr: { $lt: ["$remaining", "$capacity"] } }, is there a way to include this condition in the update argument?
The reason for this is so that I want the returned matchCount to return 1 if the name is matched.
Yes, you can do that if you use mongo 4.2+ using aggregate update.
db.collection.update({
$and: [ //condition goes here
{
name: {
$eq: "name"
}
},
],
},
[
{
"$set": { //conditional update
"remaining": {
"$switch": {
"branches": [
{
case: {
$lt: [ //condition to update
"$remaining",
"$capacity"
]
},
then: {
$add: [ //true case
"$remaining",
1
]
}
}
],
default: {
$add: [ //if no match
"$remaining",
0
]
}
}
}
}
}
])
playground
hello I have this function where I want to calculate the number of orders for each status in one array, the code is
let statusEnum = ["pending", "canceled", "completed"];
let userOrders = await Orders.aggregate([
{
$match: {
$or: [
{ senderId: new mongoose.Types.ObjectId(req.user._id) },
{ driverId: new mongoose.Types.ObjectId(req.user._id) },
{ reciverId: new mongoose.Types.ObjectId(req.user._id) },
],
},
},
{
$group: {
_id: null,
totalOrders: { $sum: 1 },
totalPendingOrders: "??", //I want to determine this for each order status
totalCompletedOrders: "??",
totalCanceledOrders: "??",
},
},
]);
so I could add add a $match and use {status : "pending"} but this will filter only the pending orders, I could also map the status enum and replace each element instead of the "pending" above and then push each iteration in another array , but that just seems so messy, is there any other way to calculate total for each order status with using only one aggregate?
thanks
You can use group as you used, but with condition
db.collection.aggregate([
{
$group: {
_id: null,
totalPendingOrders: {
$sum: { $cond: [ { $eq: [ "$status", "pending" ] }, 1, 0 ] }
},
totalCompletedOrders: {
$sum: { $cond: [ { $eq: [ "$status", "completed" ] }, 1, 0 ] }
},
totalCanceledOrders: {
$sum: { $cond: [ { $eq: [ "$status", "canceled" ] }, 1, 0 ] }
}
}
}
])
Working Mongo playground
Suppose I have a trips collection where I would like to compute the status of each trip. The status is calculated based on other fields.
If the trip has no driver it should be pending.
If the trip has a driver it should be in transit.
If the trip has a driver and an invoice number it should be completed.
If the trip has a driver and an invoice number and a bill number it should be billed.
How could I implement this using the aggregation pipeline?
Example docs :
[{
// This has both driver and invoice, should be completed
"_id" : ObjectId("5e24fbfd44621900c5730a48"),
"customer" : ObjectId("5dd7eaf7ef8a7b00ba8f090b"),
"date" : ISODate("2020-01-17T03:00:00.000Z"),
"distance" : 24, // in km
"driver" : ObjectId("5e1e302e26f00000c451923e"),
"invoice" : "0001-00001234",
"status": "completed" // this should be calculated
},
{
// This has only driver, should be in transit
"_id" : ObjectId("5e24fbfd44621900c5730a48"),
"customer" : ObjectId("5dd7eaf7ef8a7b00ba8f090b"),
"date" : ISODate("2020-01-17T03:00:00.000Z"),
"distance" : 24, // in km
"driver" : ObjectId("5e1e302e26f00000c451923e"),
"status": "in transit" // this should be calculated
},
{
// This is missing both driver and invoice, should be pending
"_id" : ObjectId("5e24fbfd44621900c5730a48"),
"customer" : ObjectId("5dd7eaf7ef8a7b00ba8f090b"),
"date" : ISODate("2020-01-17T03:00:00.000Z"),
"distance" : 24, // in km
"status": "pending" // this should be calculated
}]
You can try this, it uses $switch to check all cases in series & $type check for field existence :
db.trips.aggregate([{
$addFields: {
status: {
$switch:
{
branches: [
{
case: { $eq: [{ $type: '$driver' }, 'missing'] },
then: 'pending'
},
{
case: { $eq: [{ $type: '$driver' }, 'objectId'] },
then: 'in transit'
},
{
case: {
$and: [{ $eq: [{ $type: '$driver' }, 'objectId'] },
{ $eq: [{ $type: '$invoice' }, 'string'] }]
},
then: "completed"
},
{
case: {
$and: [{ $eq: [{ $type: '$driver' }, 'objectId'] },
{ $eq: [{ $type: '$invoice' }, 'string'] },
{ $eq: [{ $type: '$billNumber' }, 'string'] }]
},
then: "billed"
}
],
default: '$status'
}
}
}
}
])
Ref : MongoDB-Playground
One solution would be this one:
db.col.updateMany(
{
driver: { $exists: true },
invoice: { $exists: true }
},
{ $set: {status: "completed"}}
)
db.col.updateMany(
{
driver: { $exists: false },
invoice: { $exists: false }
},
{ $set: {status: "pending"}}
)
db.col.updateMany(
{
driver: { $exists: true },
invoice: { $exists: false }
},
{ $set: {status: "in transit"}}
)
Or in a single command witn Aggregation pipeline (Starting in MongoDB 4.2):
db.col.updateMany(
{},
[{
$set: {
status: {
$switch: {
branches: [
{ case: { $and: ["$driver", "$invoice"] }, then: "completed" },
{ case: { $and: ["$driver", { $not: "$invoice" }] }, then: "in transit" },
{ case: { $and: [{ $not: "$driver" }, { $not: "$invoice" }] }, then: "pending" },
],
default: "$status"
}
}
}
}]
)
I would like to search for all activity which has the same action_object.reply.id or action_target.reply.id. Something like this:
Activity
.find({ $or: [
{ 'action_object.reply.id': replyId },
{ 'action_target.reply.id': replyId }
]});
But i also only want to update the removed attribute like this:
Activity
.update({ 'action_object.reply.id': replyId }, {
'action_object.reply.removed': true }, { multi: true });
Activity
.update({ 'action_target.reply.id': replyId }, {
'action_target.reply.removed': true }, { multi: true });
Is it possible to somehow combine these two queries? I want to update action_target.reply.removed where action_target.reply.id or action_object.reply.removed where action_object.reply.id.
Or i must write two different queries for this like i did above.
The first argument to the update call is the query object, so you can simply use the same $or query. Mongo will update all documents retrieve by the query.
Activity
.update({ $or: [
{ 'action_object.reply.id': replyId },
{ 'action_target.reply.id': replyId }
]}, {'action_object.reply.removed': true }, { multi: true });
With 4.2, you can use $cond
// Configuration
[
{
"action_object": {
"reply": {
"id": "bar",
"removed": false
}
}
},
{
"action_target": {
"reply": {
"id": "foo",
"removed": false
}
}
}
]
// Query
db.collection.update({
$or: [
{
"action_object.reply.id": "foo"
},
{
"action_target.reply.id": "foo"
}
]
},
[
{
$set: {
"action_object.reply.removed": {
$cond: [
{
$eq: [
"foo",
"$action_object.reply.id"
]
},
true,
"$$REMOVE"
]
},
"action_target.reply.removed": {
$cond: [
{
$eq: [
"foo",
"$action_target.reply.id"
]
},
true,
"$$REMOVE"
]
}
}
}
],
{
multi: true
})
https://mongoplayground.net/p/tOLh5YKRVX1