I have a collection:
{
values: [null, null, 1, 2, 3, 4.6],
}
I want to receive a property which tells me if any of those values is a number.
I've tried:
{
$project: {
hasNumber: {
$in: [{ $eq: [{ $type: '$$CURRENT' }, 'number'] }, '$values'],
},
},
}
but it doesn't work, is something like this possible with aggregations?
Please try this :
db.yourCollectionName.aggregate([{
$project: {
values: 1, hasNumber: {
$gt: [{
$size: {
$filter:
{
input: "$values",
as: "value",
cond: { $eq: [{ $type: '$$value' }, 'int'] }
// To check & include double as well, replace above cond with this :
//cond: { $or :[ {$eq: [{ $type: '$$value' }, 'int']} , {$eq: [{ $type: '$$value' }, 'double']}] }
}
}
}, 0]
}
}
}])
Collection Data :
/* 1 */
{
"_id" : ObjectId("5e14d9dd627ef78236ea77e3"),
"values" : [
null,
null,
1,
2,
3,
4.6
]
}
/* 2 */
{
"_id" : ObjectId("5e14d9e4627ef78236ea785f"),
"values" : [
null,
null
]
}
/* 3 */
{
"_id" : ObjectId("5e14decc627ef78236eb12d3"),
"values" : [
"1",
4.6
]
}
Result :
/* 1 */
{
"_id" : ObjectId("5e14d9dd627ef78236ea77e3"),
"values" : [
null,
null,
1,
2,
3,
4.6
],
"hasNumber" : true
}
/* 2 */
{
"_id" : ObjectId("5e14d9e4627ef78236ea785f"),
"values" : [
null,
null
],
"hasNumber" : false
}
/* 3 */ // If we're checking for double this hasNumber will be true
{
"_id" : ObjectId("5e14decc627ef78236eb12d3"),
"values" : [
"1",
4.6
],
"hasNumber" : false
}
Debugging your code...
$in: [{ $eq: [{ $type: '$$CURRENT' }, 'number'] }, '$values'],
You are checking if false is inside $values.
Explanation:
'$$CURRENT' returns raw document
{ $type: '$$CURRENT' } returns 'object'
$eq:['object', 'number'] will always return false
$in:[ 'false', '$values'] will be false
I've solved with $convert operator
db.collection.aggregate([
{
$project: {
hasNumber: {
$cond: [
{
$eq: [
{
$map: {
input: "$values",
in: {
$convert: {
input: "$$this",
to: "int",
onError: -999,
onNull: -999
}
}
}
},
"$values"
]
},
true,
false
]
}
}
}
])
Related
I have this document:
{
"_id" : ObjectId("626c0440e1b4f9bb5568f542"),
"ap" : [
{
"ap_id" : ObjectId("000000000000000000000001"),
"shop_prices" : [
{
"shop_id" : ObjectId("000000000000000000000097"),
"price" : 102
}
]
}
],
"bc" : [
{
"bc_id" : ObjectId("000000000000000000000003"),
"price" : 102
},
{
"bc_id" : ObjectId("000000000000000000000004"),
"price" : 104
}
],
"stock_price" : 70
}
My need is to eventually add to ap.shop_prices an element if not exists with this structure:
{
"shop_id" : ObjectId("000000000000000000000096"),
"price" : 104
}
where the price is bc.price where bc.bc_id = ObjectId("000000000000000000000004")
This is my first (unsuccesfull) try:
updateMany(
{
"_id": {"$eq": ObjectId("626c0421e1b4f9bb5568f531")},
"ap":{
$elemMatch:{
"ap_id":{$in:[ObjectId("000000000000000000000001")]},
"shop_prices.shop_id":{$ne:ObjectId("000000000000000000000096")}
}
},
"bc.bc_id": ObjectId("000000000000000000000003")
},
[
{"$set":
{"ap.$.shop_prices":
{"$cond":
[{"$in": [ObjectId("000000000000000000000096"), "$ap.$.shop_prices.shop_id"]}, "$ap.$.shop_prices",
{"$concatArrays":
["$ap.$.shop_prices",
[{"shop_id": ObjectId("000000000000000000000096"), "price": ???}]
]
}
]
}
}
}
]
)
thanks in advance
You can do that:
finding the bc related to your request using the $project
using $map in the $set operator
This should be the solution:
db.getCollection('test').update({
"ap": {
$elemMatch: {
"ap_id":{$in:[ObjectId("000000000000000000000001")]},
"shop_prices.shop_id":{$ne:ObjectId("000000000000000000000096")}
}
},
"bc.bc_id": ObjectId("000000000000000000000004")
},
[
{
$project: {
ap: 1,
bc: 1,
stock_price: 1,
current_bc: {
$arrayElemAt: [ {
$filter: {
input: "$bc",
as: "curr_bc",
cond: {$eq: ["$$curr_bc.bc_id", ObjectId("000000000000000000000004")]}
}
}, 0 ]
}
}
},
{
$set: {
"ap": {
"$map": {
input: "$ap",
as: "current_ap",
in: {
$cond: [
{$eq: [ObjectId("000000000000000000000001"), "$$current_ap.ap_id"]},
{
"$mergeObjects": [
"$$current_ap",
{"shop_prices": {$concatArrays: ["$$current_ap.shop_prices", [{"shop_id": ObjectId("000000000000000000000096"), "price": "$current_bc.price"}]]}}
]
},
"$$current_ap"
]
}
}
}
}
}
])
In the below collection, column "qty" holds the integer values but the datatype is string.
I want to compare the "qty" field with an integer in the aggregate and "warehouse" field with a string "A". ("qty" > 2 and "warehouse" = "A")
[Can't change the datatype in the collection to integer as huge dependency is present]
Edit : Need to retrieve all the columns and all the documents matching the criteria.
Query : getting improper results
db.runCommand(
{
aggregate: "products", pipeline: [
{
$match: {
instock: {
$elemMatch: {
warehouse: "A",
qty: { $gt: "2" }
}
}
}
},
{ $project: { _id: 0 } }],
cursor: { batchSize: 200 }
});
Result : not getting documents where item = journal though it satisfies the conditions
/* 1 */
{
"item" : "paper",
"instock" : [
{
"warehouse" : "A",
"qty" : "60"
},
{
"warehouse" : "B",
"qty" : "15"
}
]
},
/* 2 */
{
"item" : "planner",
"instock" : [
{
"warehouse" : "A",
"qty" : "22"
},
{
"warehouse" : "B",
"qty" : "5"
}
]
}
Products Collection
[
{
"item": "journal",
"instock": [
{
"warehouse": "A",
"qty": "11"
},
{
"warehouse": "C",
"qty": "15"
}
]
},
{
"item": "paper",
"instock": [
{
"warehouse": "A",
"qty": "60"
},
{
"warehouse": "B",
"qty": "15"
}
]
},
{
"item": "planner",
"instock": [
{
"warehouse": "A",
"qty": "22"
},
{
"warehouse": "B",
"qty": "5"
}
]
}
]
Getting improper results as greater than operator in this case is working lexicographically but it should work like integers. Though I tried converting that to double but I am getting no results.
Query with $convert to double : no result
db.runCommand(
{
aggregate: "products", pipeline: [
//{ $match: { "item": { $in: ["planner", "paper","journal"] } } },
{
$match: {
instock: {
$elemMatch: {
warehouse: "A",
qty: {
$gt: [
{$convert:{ input: "$qty", to: "double" }}, 5]
}
}
}
}
},
{ $project: { _id: 0 } }],
cursor: { batchSize: 200 }
});
Try this:
db.products.aggregate([
{
$unwind: "$instock"
},
{
$match: {
$expr: {
$and: [
{
$eq: [
"$instock.warehouse",
"A"
]
},
{
$gt: [
{
$toInt: "$instock.qty"
},
2
]
}
]
}
}
},
{
$group: {
_id: "$_id",
item: {
$first: "$item"
},
instock: {
$push: "$instock"
}
}
},
{
$project: {
_id: 0
}
}
])
MongoPlayground
Try this, it uses $filter to retain objects has criteria :
db.runCommand(
{
aggregate: "products", pipeline: [
{ $match: { 'instock.warehouse': 'A' } },
{
$addFields: {
instockCheck: {
$filter: {
input: '$instock', as: 'each', cond: {
$and: [{ $gt: [{ $toInt: '$$each.qty' }, 2] },
{ $eq: ['$$each.warehouse', 'A'] }]
}
}
}
}
}, { $match: { instockCheck: { $gt: [] } } }, { $project: { instockCheck: 0, _id: 0 } }],
cursor: { batchSize: 200 }
});
Test : MongoDB-Playground
I am trying to create an aggregation MongoDB query.
Structure of data:
{
"object_name": Example,
"values": [ {"name":"value1", "value":1},
{"name":"value2", "value":10},
{"name":"total", "value":105}
}
Goal: Find object names where value1/total > 0.5 and value2/total > 0.25 and total > 100.
The data is structured in this way to provide indexes on the value_name and value fields.
What I tried - aggregate with the following pipelines:
$match: filter documents with total > 100:
$match: { values: { $elemMatch: { value_name: "total", value: {$gte: 100 }
$project: grab only the value_names that we need (there are close to 200 different names)
$project: {
values: {
$filter: {
input: "$values",
as: "value",
cond: { $or: [
{ $eq: [ "$$value.name", "name1"] },
{ $eq: [ "$$value.name", "name2"] },
{ $eq: [ "$$value.name", "total"] },
] }
}
},
name: 1
}
then, { $unwind: "$values" }
And here, I could $group to $divide: name1/total, name2/total however I'm stuck on how to get those values.
I can't simply do stats.value: because it does not know which value I'm referring to. I believe $group can't do $elemMatch to also match the name.
If there are simpler solutions that this, I'd greatly appreciate your input.
Please try this :
We're filtering documents where values array has an object with
name : total & value > 100.
Adding object with name : total
to document.
Leaving only objects that match with criteria
value1/total > 0.5 and value2/total > 0.25 in values array.
If
size of that array is greater than 1, then those two conditions are
met.
Finally projecting only object_name
Query :
db.yourCollectionName.aggregate([{ $match: { values: { $elemMatch: { name: "total", value: { $gte: 100 } } } } },
{
$addFields: {
totalValue: {
$arrayElemAt: [{
$filter: {
input: "$values",
as: "item",
cond: { $eq: ["$$item.name", 'total'] }
}
}, 0]
}
}
},
{
$project: {
values: {
$filter: {
input: "$values",
as: "value",
cond: {
$or: [
{ $cond: [{ $eq: ["$$value.name", "value1"] }, { $gt: [{ $divide: ["$$value.value", '$totalValue.value'] }, 0.5] }, false] },
{ $cond: [{ $eq: ["$$value.name", "value2"] }, { $gt: [{ $divide: ["$$value.value", '$totalValue.value'] }, 0.25] }, false] }
]
}
}
}, object_name: 1
}
}, {
$match: {
$expr: { $gt: [{ $size: "$values" }, 1] }
}
}, { $project: { object_name: 1, _id: 0 } }])
Collection Data :
/* 1 */
{
"_id" : ObjectId("5e20bd94d02e05b694d55fa5"),
"object_name" : "Example",
"values" : [
{
"name" : "value1",
"value" : 1
},
{
"name" : "value2",
"value" : 10
},
{
"name" : "total",
"value" : 105
},
{
"name" : "total1",
"value" : 105
}
]
}
/* 2 */
{
"_id" : ObjectId("5e20bdb1d02e05b694d56490"),
"object_name" : "Example2",
"values" : [
{
"name" : "value1",
"value" : 1
},
{
"name" : "value2",
"value" : 10
},
{
"name" : "total",
"value" : 5
},
{
"name" : "total1",
"value" : 5
}
]
}
/* 3 */
{
"_id" : ObjectId("5e20d1b7d02e05b694d7c57a"),
"object_name" : "Example3",
"values" : [
{
"name" : "value1",
"value" : 100
},
{
"name" : "value2",
"value" : 100
},
{
"name" : "total",
"value" : 200
},
{
"name" : "total1",
"value" : 205
}
]
}
/* 4 */
{
"_id" : ObjectId("5e20d1cad02e05b694d7c71c"),
"object_name" : "Example4",
"values" : [
{
"name" : "value1",
"value" : 200
},
{
"name" : "value2",
"value" : 40
},
{
"name" : "total",
"value" : 200
},
{
"name" : "total1",
"value" : 205
}
]
}
/* 5 */
{
"_id" : ObjectId("5e20d1e2d02e05b694d7c933"),
"object_name" : "Example5",
"values" : [
{
"name" : "value1",
"value" : 150
},
{
"name" : "value2",
"value" : 100
},
{
"name" : "total",
"value" : 200
},
{
"name" : "total1",
"value" : 205
}
]
}
Result :
/* 1 */
{
"object_name" : "Example5"
}
You may convert your array into object with $arrayToObject operator and add tmp field to have easy access to value1, value2, total values
db.collection.aggregate([
{
$addFields: {
tmp: {
$arrayToObject: {
$map: {
input: "$values",
as: "value",
in: {
k: "$$value.name",
v: "$$value.value"
}
}
}
},
name: 1
}
},
{
$match: {
$expr: {
$and: [
{
$gt: [
{
$divide: [
"$tmp.value1",
"$tmp.total"
]
},
0.5
]
},
{
$gt: [
{
$divide: [
"$tmp.value2",
"$tmp.total"
]
},
0.25
]
},
{
$gt: [
"$tmp.total",
100
]
}
]
}
}
},
{
$project: {
tmp: 0
}
}
])
MongoPlayground
I have this sample mongodb document -
{
_id: 5db85ee97d9fb13ead4fc54c
applId: 5d48f34f7d9fb10ce171f905
fileId: "dd386cf7-4139-45c2-9853-cbb126621b51"
job: Object
country: "US"
fullName: "abcd xyz"
htmlWordCount: 2766
textWordCount: 1867
rchilliTextWordCount: 2840
deleted: 0
dateEntered: 2019-10-29 15:46:49.237
dateModified: 2019-10-29 15:46:49.237
}
I want to build a query in compass so that I have following fields in the output -
{
_id: 5db85ee97d9fb13ead4fc54c
country: "US"
fullName: "abcd xyz"
htmlWordCount: 2766
textWordCount: 1867
rchilliTextWordCount: 2840
winner: "rchilliTextWordCount"
}
Please note that it has a new field called "winner" which always returns the column with maximum wordcount (out of 3 "htmlWordCount", "textWordCount", "rchilliTextWordCount" columns). This new column "winner" is to be produced on runtime on query. Also this query is filtered on country = "US".
How do I do this in MongoDB Compass or what should the aggregation pipeline look like?
You may use $switch or $cond
db.collection.aggregate([
{
$match: {
country: "US"
}
},
{
$project: {
country: 1,
fullName: 1,
htmlWordCount: 1,
textWordCount: 1,
rchilliTextWordCount: 1,
winner: {
$switch: {
branches: [
{
case: {
$and: [
{
$gt: [
"$htmlWordCount",
"$textWordCount"
]
},
{
$gt: [
"$htmlWordCount",
"$rchilliTextWordCount"
]
}
]
},
then: "htmlWordCount"
},
{
case: {
$and: [
{
$gt: [
"$textWordCount",
"$htmlWordCount"
]
},
{
$gt: [
"$textWordCount",
"$rchilliTextWordCount"
]
}
]
},
then: "textWordCount"
},
{
case: {
$and: [
{
$gt: [
"$rchilliTextWordCount",
"$htmlWordCount"
]
},
{
$gt: [
"$rchilliTextWordCount",
"$textWordCount"
]
}
]
},
then: "rchilliTextWordCount"
}
],
default: "No winners"
}
}
}
}
])
MongoPlayground
This is another approach of getting the result:
Get the document's field names and their values
Find the maximum value for the fields with names in [ "htmlWordCount", "textWordCount", "rchilliTextWordCount" ].
In general, finding a maximum value from an array is a kind of reduction; so I used the $reduce in this case. Note the code is simpler. In case you want add another field for calculating the maximum, just add it to the array.
db.winner.aggregate([
{ $match: { country: "US"} },
{ $addFields: { fieldNameValues: { "$objectToArray": "$$ROOT" } } },
{ $project: { _id: 1, country: 1, fullName: 1, htmlWordCount: 1, textWordCount: 1, rchilliTextWordCount: 1,
winner: {
$reduce: {
input: "$fieldNameValues",
initialValue: { },
in: {
$cond: [
{ $and: [
{ $in: [ "$$this.k", [ "htmlWordCount", "textWordCount", "rchilliTextWordCount" ] ] },
{ $gt: [ "$$this.v", "$$value.v"] } ]
},
"$$this",
"$$value"
]
}
}
}
} },
{ $addFields: { winner: "$winner.k" } }
] )
[ EDIT ADD ]
Sample Data and Result:
{
"_id" : 1,
"fileId" : "dd386cf7-4139-45c2-9853-cbb126621b51",
"job" : { },
"country" : "US",
"fullName" : "abcd xyz",
"htmlWordCount" : 2766,
"textWordCount" : 1867,
"rchilliTextWordCount" : 2840
}
{
"_id" : 2,
"fileId" : "dd386cf7-4139-45c2-9853-cbb126621b51",
"job" : { },
"country" : "US",
"fullName" : "lmn opqrs",
"htmlWordCount" : 5,
"textWordCount" : 9,
"rchilliTextWordCount" : 2
}
Output:
{
"_id" : 1,
"country" : "US",
"fullName" : "abcd xyz",
"htmlWordCount" : 2766,
"textWordCount" : 1867,
"rchilliTextWordCount" : 2840,
"winner" : "rchilliTextWordCount"
}
{
"_id" : 2,
"country" : "US",
"fullName" : "lmn opqrs",
"htmlWordCount" : 5,
"textWordCount" : 9,
"rchilliTextWordCount" : 2,
"winner" : "textWordCount"
}
I have a collection in MongoDB that looks something like the following:
{ "_id" : 1, "type" : "start", userid: "101", placementid: 1 }
{ "_id" : 2, "type" : "start", userid: "101", placementid: 2 }
{ "_id" : 3, "type" : "start", userid: "101", placementid: 3 }
{ "_id" : 4, "type" : "end", userid: "101", placementid: 1 }
{ "_id" : 5, "type" : "end", userid: "101", placementid: 2 }
and I want to group results by userid then placementid and then count the types of "start" and "end", but only when the two counts are different. In this particular example I would want to get placementid: 3 because when grouped and counted this is the only case where the counts don't match.
I've written a query that gets the 2 counts and the grouping but I can't do the filtering when counts don't match. This is my query:
db.getCollection('mycollection').aggregate([
{
$project: {
userid: 1,
placementid: 1,
isStart: {
$cond: [ { $eq: ["$type", "start"] }, 1, 0]
},
isEnd: {
$cond: [ { $eq: ["$type", "end"] }, 1, 0]
}
}
},
{
$group: {
_id: { userid:"$userid", placementid:"$placementid" },
countStart:{ $sum: "$isStart" },
countEnd: { $sum: "$isEnd" }
}
},
{
$match: {
countStart: {$ne: "$countEnd"}
}
}
])
It seems like I'm using the match aggregation incorrectly because I'm seeing results where countStart and countEnd are the same.
{ "_id" : {"userid" : "101", "placementid" : "1"}, "countStart" : 1.0, "countEnd" : 1.0 }
{ "_id" : {"userid" : "101", "placementid" : "2"}, "countStart" : 1.0, "countEnd" : 1.0 }
{ "_id" : {"userid" : "101", "placementid" : "3"}, "countStart" : 1.0, "countEnd" : 0 }
Can anybody point into the right direction please?
To compare two fields inside $match stage you need $expr which is available in MongoDB 3.6:
db.myCollection.aggregate([
{
$project: {
userid: 1,
placementid: 1,
isStart: {
$cond: [ { $eq: ["$type", "start"] }, 1, 0]
},
isEnd: {
$cond: [ { $eq: ["$type", "end"] }, 1, 0]
}
}
},
{
$group: {
_id: { userid:"$userid", placementid:"$placementid" },
countStart:{ $sum: "$isStart" },
countEnd: { $sum: "$isEnd" }
}
},
{
$match: {
$expr: { $ne: [ "$countStart", "$countEnd" ] }
}
}
])
If you're using older version of MongoDB you can use $redact:
db.myCollection.aggregate([
{
$project: {
userid: 1,
placementid: 1,
isStart: {
$cond: [ { $eq: ["$type", "start"] }, 1, 0]
},
isEnd: {
$cond: [ { $eq: ["$type", "end"] }, 1, 0]
}
}
},
{
$group: {
_id: { userid:"$userid", placementid:"$placementid" },
countStart:{ $sum: "$isStart" },
countEnd: { $sum: "$isEnd" }
}
},
{
$redact: {
$cond: { if: { $ne: [ "$countStart", "$countEnd" ] }, then: "$$KEEP", else: "$$PRUNE" }
}
}
])
You run do the following pipeline to get this - no need to use $expr or $redact or anything special really:
db.mycollection.aggregate({
$group: {
_id: {
"userid": "$userid",
"placementid": "$placementid"
},
"sum": {
$sum: {
$cond: {
if: { $eq: [ "$type", "start" ] },
then: 1, // +1 for start
else: -1 // -1 for anything else
}
}
}
}
}, {
$match: {
"sum": { $ne: 0 } // only return the non matching-up ones
}
})