How to use $geoNear with $lookup in mongodb aggregation - mongodb

In my MongoDB aggregation query, I am using $lookup to join my offers collection with outlet collection. But, in my "outlets" collection, I have one field named location and i want the query to sort the results from closest to farthest of that location. So, how to use $geoNear with $lookup, any help would be appreciated? Below is my query:
db.offers.aggregate([
{
$geoNear: {
near: {
type: "Points",
coordinates: [
22,
77
]
},
distanceField: "distance",
maxDistance: 5000,
spherical: true
}
},
{
$match: {
$and: [
{
'totalDiscount': {
$gt: 40
}
},
{
'totalDiscount': {
$lt: 60
}
}
]
}
},
{
$unwind: "$storeUuid"
},
{
$lookup: {
from: "outlets",
localField: "storeUuid",
foreignField: "uuid",
as: "store"
}
},
{
$project: {
_id: 0,
location1: {
$arrayElemAt: [
"$store.location",
0
]
}
}
},
{
$addFields: {
'location.latitude': {
$ifNull: [
{
$arrayElemAt: [
"$location1.coordinates",
1
]
},
0
]
},
'location.longitude': {
$ifNull: [
{
$arrayElemAt: [
"$location1.coordinates",
0
]
},
0
]
}
}
},
{
$sort: {
location: 1
}
}
])
Offer data model
{
"offerId": "6e9d595a-16ad-4c6c-93d9-a7edc2bbb56f",
"brandUuid": [
"5b198438-8b4c-46f0-8cc2-6a938cb41d8e"
],
"storeUuid": [
"33ca653e-2af0-4728-b4a0-1178565c2b40",
"1b383916-8856-4f5a-8761-4bd4585e1d71"
],
"totalDiscount": 50
}
Outlet data model
{
"uuid": "20389cc1-2791-4d7b-a603-75b7abd6d48a",
"location": {
"type": "Point",
"coordinates": [
77.6504768,
12.9176082
]
}
},
EDIT: Based on Waqas Noor's answer
Actual Result
{
"offers": [
{
"uuid": "33ca653e-2af0-4728-b4a0-1178565c2b40",
"distance": 2780.7979952350124,
"offerId": "6e9d595a-16ad-4c6c-93d9-a7edc2bbb56f"
},
{
"uuid": "b4768792-a927-4d65-91a3-8ad67ad217b2",
"distance": 3930.1660094190306,
"offerId": "4f71fe98-cb43-4134-b360-b32017981de1"
},
{
"uuid": "1dbac2d2-b326-4d6d-8d74-9df99f35f542",
"distance": 3973.3702922423313,
"offerId": "070b916c-dd4d-42b4-b886-74318f576ffb"
},
{
"uuid": "20389cc1-2791-4d7b-a603-75b7abd6d48a",
"distance": 4107.770111767324,
"offerId": "0f037c18-a58f-4b03-b0f4-db8e2d971b74"
},
{
"uuid": "20389cc1-2791-4d7b-a603-75b7abd6d48a",
"distance": 4107.770111767324,
"offerId": "070b916c-dd4d-42b4-b886-74318f576ffb"
},
{
"uuid": "2f968cfa-1bf1-4344-bc73-998f4974f58a",
"distance": 4165.187832520325,
"offerId": "4f71fe98-cb43-4134-b360-b32017981de1"
},
{
"uuid": "3cc1461f-f29b-4744-a540-69d24ebb98a8",
"distance": 4262.636071210964,
"offerId": "0f037c18-a58f-4b03-b0f4-db8e2d971b74"
},
{
"uuid": "3cc1461f-f29b-4744-a540-69d24ebb98a8",
"distance": 4262.636071210964,
"offerId": "070b916c-dd4d-42b4-b886-74318f576ffb"
},
{
"uuid": "1b383916-8856-4f5a-8761-4bd4585e1d71",
"distance": 4361.786323018647,
"offerId": "6e9d595a-16ad-4c6c-93d9-a7edc2bbb56f"
},
{
"uuid": "7af0e1f8-d4d6-4700-adea-1df07a029f56",
"distance": 4564.666204168865,
"offerId": "8bbb5e27-89ff-417f-8312-f70e3911cb4c"
}
]
}
Expected Result
{
"offers": [
{
"uuid": "33ca653e-2af0-4728-b4a0-1178565c2b40",
"distance": 2780.7979952350124,
"offerId": "6e9d595a-16ad-4c6c-93d9-a7edc2bbb56f"
},
{
"uuid": "b4768792-a927-4d65-91a3-8ad67ad217b2",
"distance": 3930.1660094190306,
"offerId": "4f71fe98-cb43-4134-b360-b32017981de1"
},
{
"uuid": "1dbac2d2-b326-4d6d-8d74-9df99f35f542",
"distance": 3973.3702922423313,
"offerId": "070b916c-dd4d-42b4-b886-74318f576ffb"
},
{
"uuid": "20389cc1-2791-4d7b-a603-75b7abd6d48a",
"distance": 4107.770111767324,
"offerId": "0f037c18-a58f-4b03-b0f4-db8e2d971b74"
},
{
"uuid": "2f968cfa-1bf1-4344-bc73-998f4974f58a",
"distance": 4165.187832520325,
"offerId": "4f71fe98-cb43-4134-b360-b32017981de1"
},
{
"uuid": "3cc1461f-f29b-4744-a540-69d24ebb98a8",
"distance": 4262.636071210964,
"offerId": "0f037c18-a58f-4b03-b0f4-db8e2d971b74"
},
{
"uuid": "1b383916-8856-4f5a-8761-4bd4585e1d71",
"distance": 4361.786323018647,
"offerId": "6e9d595a-16ad-4c6c-93d9-a7edc2bbb56f"
},
{
"uuid": "7af0e1f8-d4d6-4700-adea-1df07a029f56",
"distance": 4564.666204168865,
"offerId": "8bbb5e27-89ff-417f-8312-f70e3911cb4c"
}
]
}

1) You need to have 2dsphare index on outlet collection on field location.
You can make one using:
db.outlet.createIndex( {location : "2dsphere" } )
2) You have to run aggregation on outlet collection since it contains the location field and you can only use $geoNear as first stage of pipeline.
Your query will look like
db.outlet.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [ 77.6504768,
12.9176088] },
distanceField: "distance",
includeLocs: "location",
spherical: true
}
}])
3) Then you can combine the offers in your outlets using $lookup Operator.
Your complete query will look something like
db.outlet.aggregate([
{
$geoNear: {
near: {
type: "Point", coordinates: [77.6504768,
12.9176088]
},
distanceField: "distance",
includeLocs: "location",
spherical: true
}
},
{ $project: { uuid: 1, distance: 1 } },
{
$lookup: {
from: "offers",
localField: "uuid",
foreignField: "storeUuid",
as: "offers"
}
},
{ $unwind: '$offers' },
{
$match: {
'offers.totalDiscount': {
$gt: 40,
$lt: 60
}
}
},
{ $sort: { distance: -1 } }
])

Related

MongoDB + BBOX + Point = inside

I have the following document in my MongoDB collection, which I would like to be able to do a query that check if point that is provided by the user is inside a bbox array that is stored in the collection.
{
"type": "Feature",
"properties": {
"place_id": 298104298,
"osm_type": "relation",
"osm_id": 80500,
"display_name": "Australia",
"place_rank": 4,
"category": "boundary",
"type": "administrative",
"importance": 0.8521350639151115,
"address": {
"country": "Australia",
"country_code": "au"
}
},
"bbox": [
72.2461932,
-55.3228175,
168.2261259,
-9.0880125
]
}
What I would like to do is a geoIntersect or geoWithin query.
for example:
[{
$match: {
bbox: {
$nearSphere: {
$geometry: {
type: 'Point',
coordinates: [
-73.9667,
40.78
]
}
}
}
}
}]
I have also tried
[{
$project: {
geometry: 0
}
}, {
$match: {
bbox: {
$geoWithin: {
$box: [
[
-73.9667,
40.78
],
[
40.78,
-73.9667
]
]
}
}
}
}]
However that did return results but wrong results the geo location should return NULL as the location is Antarctic Ice shield, Antarctica - (-73.9667,40.78)

Filter documents that have id in another collection in MongoDB with aggregation framework

So I have two collection. collectionA and collectionB
collection A has following documents
db={
"collectiona": [
{
"_id": "6173ddf33ed09368a094e68a",
"title": "a"
},
{
"_id": "61wefdf33ed09368a094e6dc",
"title": "b"
},
{
"_id": "61wefdfewf09368a094ezzz",
"title": "c"
},
],
"collectionb": [
{
"_id": "6173ddf33ed0wef368a094zq",
"collectionaID": "6173ddf33ed09368a094e68a",
"data": [
{
"userID": "123",
"visibility": false,
"response": false
},
{
"userID": "2345",
"visibility": true,
"response": true
}
]
},
{
"_id": "6173ddf33ed09368awef4e68g",
"collectionaID": "61wefdf33ed09368a094e6dc",
"data": [
{
"userID": "5678",
"visibility": false,
"response": false
},
{
"userID": "674",
"visibility": true,
"response": false
}
]
}
]
}
So What I need is documents from collection A which has response false in collection B
and document should be sorted by first the ones that have visibility false and then the ones that have visibility true
for eg. userID : 123 should get 3 documents
{
"_id": "6173ddf33ed09368a094e68a",
"title": "a"
},
{
"_id": "61wefdf33ed09368a094e6dc",
"title": "b"
},
{
"_id": "61wefdfewf09368a094ezzz",
"title": "c"
},
whereas userID 2345 should get two
{
"_id": "61wefdf33ed09368a094e6dc",
"title": "b"
},
{
"_id": "61wefdfewf09368a094ezzz",
"title": "c"
},
User 674 will receive 3 objects from collection A but second would be in the last as it has visibility true for that document
{
"_id": "6173ddf33ed09368a094e68a",
"title": "a"
},
{
"_id": "61wefdfewf09368a094ezzz",
"title": "c"
},
{
"_id": "61wefdf33ed09368a094e6dc",
"title": "b"
},
MongoDB Playground link : https://mongoplayground.net/p/3rLry0FPlw-
Really appreciate the help. Thanks
You can start from collectionA:
$lookup the collectionB for the record related to the user specified
filter out collectionB documents according to response
assign a helper sortrank field based on the visibility and whether collectionaID is a match
$sort according to sortrank
wrangle back to the raw collection A
db.collectiona.aggregate([
{
"$lookup": {
"from": "collectionb",
let: {
aid: "$_id"
},
"pipeline": [
{
$unwind: "$data"
},
{
$match: {
$expr: {
$and: [
{
$eq: [
"$data.userID",
"2345"
]
},
{
$eq: [
"$collectionaID",
"$$aid"
]
}
]
}
}
}
],
"as": "collB"
}
},
{
$match: {
"collB.data.response": {
$ne: true
}
}
},
{
"$unwind": {
path: "$collB",
preserveNullAndEmptyArrays: true
}
},
{
"$addFields": {
"sortrank": {
"$cond": {
"if": {
$eq: [
"$collB.data.visibility",
false
]
},
"then": 1,
"else": {
"$cond": {
"if": {
$eq: [
"$collB.collectionaID",
"$_id"
]
},
"then": 3,
"else": 2
}
}
}
}
}
},
{
$sort: {
sortrank: 1
}
},
{
$project: {
collB: false,
sortrank: false
}
}
])
Here is the Mongo playground for your reference.

Mongo Sort Aggregation not working on Sub Document

Trying to sort the sub-key on the document.
Example of data from the pipeline. Incorrect sort order for subkey availability.startIso
{
"_id": "60e458d7b896de9c8e44d6c9",
"uid": "6233ed1d8b154aa79d1435b5",
"name": "Pale",
"phoneNumber": "+19999813917",
"profileMedia": {
"url": "https://storage.googleapis.com/refresh-me-dev.appspot.com/dummy_photos/dummy_1.jpg",
"type": "photo"
},
"createdIso": "2021-07-06T13:21:27.513Z",
"isDeleted": false,
"isFlagged": false,
"isBanned": false,
"isAdmin": false,
"isVendor": true,
"lastOpenedAppIso": "2021-07-06T13:21:27.513Z",
"vendorMeta": {
"servicesOffered": [
"swedish"
],
"location": [
0,
0
]
},
"distanceFromPoint": 0,
"availability": [
{
"_id": "60e458d7b896de9c8e44d6cc",
"uid": "dec97d4b1dea44f7b2fa45a5",
"vendorUid": "6233ed1d8b154aa79d1435b5",
"startIso": "2021-07-12T04:07:21.349Z",
"endIso": "2021-07-12T05:07:21.360Z"
},
{
"_id": "60e458d7b896de9c8e44d6ce",
"uid": "a5928ea5c18c4321bd6a9a9b",
"vendorUid": "6233ed1d8b154aa79d1435b5",
"startIso": "2021-07-11T01:52:18.323Z",
"endIso": "2021-07-11T02:52:18.335Z"
}
]
}
Example of the aggregation
let vendors = await mongoDb
.collection<User>(collectionNames.users)
.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [lat, lng] },
spherical: true,
maxDistance: 7500,
distanceField: "distanceFromPoint",
},
},
{
$match: { isVendor: true },
},
{
$match: { "vendorMeta.servicesOffered": { $in: services } },
},
{
$lookup: {
from: "vendor.availability",
localField: "uid",
foreignField: "vendorUid",
as: "availability",
},
},
{
$addFields: {
availability: {
$filter: {
input: "$availability",
as: "availability",
cond: { $and: [{ $gte: ["$$availability.startIso", nowIso] }, { $lte: ["$$availability.endIso", nDaysIso] }] },
},
},
},
},
{ $sort: { "availability.startIso": 1 } },
{ $match: { availability: { $ne: [] } } },
])
.toArray();
This is working as intended, $sort does not work on arrays and can't be used like this. What you can do is $unwind, then $sort and end by $grouping to restore the structure, like so:
[
// ...,
{
$unwind: "$availability"
},
{ $sort: { "availability.startIso": 1 } },
{
$group: {
_id: '$_id',
root: {$first: "$$ROOT"},
availability: {$push: '$availability'}
}
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: [
'$root',
{ availability: '$availability'}
]
}
}
}
]
Note that i removed the :
{ $match: { availability: { $ne: [] } } },
As it's no longer required because $unwind will remove those documents for you.

Search location using $near and $geometry in $lookup resulted field

The scenario is Employee and they are working in multiple Store Locations, and combine both collections, that is working perfectly,
Problem in: Search in particular location (input lat, long) and this query will give that particular employee working on particular store and that location field is in lookup collection.
Employee
{
"_id": ObjectId("5f03064b3460ef1f10ec2f25"),
"employeeName": "Anonymous",
"stores": [
{
"storeId": ObjectId("5f03030a3460ef1f10ec2f23"),
"workTime": "09:30 to 12:30"
},
{
"storeId": ObjectId("5f03064b3460ef1f10ec2f26"),
"workTime": "01:30 to 02:30"
}
]
}
Store
{
"_id": ObjectId("5f03030a3460ef1f10ec2f23"),
"storeName": "A",
"location": {
"coordinates": ["longitude", "latitude"]
}
}
{
"_id": ObjectId("5f03064b3460ef1f10ec2f26"),
"storeName": "B",
"location": {
"coordinates": ["longitude", "latitude"]
}
}
The below is final result that i am getting successfully:
{
"_id": ObjectId("5f03064b3460ef1f10ec2f25"),
"employeeName": "Anonymous",
"stores": [
{
"storeId": ObjectId("5f03030a3460ef1f10ec2f23"),
"workTime": "09:30 to 12:30",
"storeLocation": {
"storeName": "A",
"location": {
"coordinates": ["longitude", "latitude"]
}
}
},
{
"storeId": ObjectId("5f03064b3460ef1f10ec2f26"),
"workTime": "01:30 to 02:30",
"storeLocation": {
"storeName": "B",
"location": {
"coordinates": ["longitude", "latitude"]
}
}
}
]
}
Aggregation
db.Enmployee.aggregate([
{ "$unwind": "$stores" },
{
"$lookup": {
"from": "Store",
"localField": "stores.storeId",
"foreignField": "_id",
"as": "stores.storeLocation"
}
},
{ "$unwind": "$stores.storeLocation" },
{
"$group": {
"_id": "$_id",
"root": { "$mergeObjects": "$$ROOT" },
"stores": { "$push": "$stores" }
}
},
{
"$replaceRoot": {
"newRoot": {
"$mergeObjects": ["$root", "$$ROOT"]
}
}
},
// <== below $match query will come here
{
"$project": {
"_id": 1,
"employeeName": 1,
"stores.workTime": 1,
"stores.storeLocation.storeName": 1,
"stores.storeLocation.location": 1
}
}
]
)
My goal is to search employee, they are working on particular location of stores using its latitude and longitude, i have tried below query in above aggregation.
{
"$match": {
"stores.storeLocation.location": {
"$near": {
"$maxDistance": 1000,
"$geometry": {
"type": "Point",
"coordinates": ["Input Longitude", "Input Latitude"]
}
}
}
}
}
It gives an error:
Failed to execute script.
Error: command failed: {
"ok" : 0,
"errmsg" : "$geoNear, $near, and $nearSphere are not allowed in this context",
"code" : 2,
"codeName" : "BadValue"
} : aggregate failed
Details:
_getErrorWithCode#src/mongo/shell/utils.js:25:13
doassert#src/mongo/shell/assert.js:18:14
_assertCommandWorked#src/mongo/shell/assert.js:534:17
assert.commandWorked#src/mongo/shell/assert.js:618:16
DB.prototype._runAggregate#src/mongo/shell/db.js:260:9
DBCollection.prototype.aggregate#src/mongo/shell/collection.js:1062:12
DBCollection.prototype.aggregate#:1:355
#(shell):1:1
$geoNear is a pipeline stage itself and you can not use it inside $match. Also, it should be the first stage of the pipeline, so you need to declare it in the $lookup pipeline.
Note that you also need to have a geo index for that use:
db.getCollection('Store').createIndex( { "location.coordinates" : "2dsphere" } )
After the changes mentioned above, the query will look like as below:
db.Employee.aggregate([
{ "$unwind": "$stores" },
{
"$lookup": {
from: "Store",
let: {
storeId: "$stores.storeId"
},
pipeline: [
{ $geoNear: {
includeLocs: "location",
distanceField: "distance",
near: {type: 'Point', coordinates: [57, 35]},
maxDistance: 1000,
spherical: true}},
{
$match: {
$expr: { $eq: ["$$storeId", "$_id"] }
}
}
],
as: "stores.storeLocation"
}
},
{ "$unwind": "$stores.storeLocation" },
{
"$group": {
"_id": "$_id",
"root": { "$mergeObjects": "$$ROOT" },
"stores": { "$push": "$stores" }
}
},
{
"$replaceRoot": {
"newRoot": {
"$mergeObjects": ["$root", "$$ROOT"]
}
}
},
// <== below $match query will come here
{
"$project": {
"_id": 1,
"employeeName": 1,
"stores.workTime": 1,
"stores.storeLocation.storeName": 1,
"stores.storeLocation.location": 1
}
}
]
)
You can findout more about $geoNear options here.

Mongodb multi level aggregation

Data in mongo
[{
"_id": "5d71d1432f7c8151c58c4481",
"payment": {
"transactions": [
{
"_id": "5d71d1ff2f7c8151c58c44cf",
"method": "paytm",
"amount": 100,
"paymentOn": "2019-09-06T03:26:44.959Z"
},
{
"_id": "5d71d1ff2f7c8151c58c44ce",
"method": "cash",
"amount": 650,
"paymentOn": "2019-09-06T03:26:55.531Z"
}
],
"status": "partial"
},
"customer": "5d66c434c24f2b1fb6772014",
"order": {
"orderNumber": "WP-ORD-06092019-001",
"total": 770,
"balance": 20
}
},
{
"_id": "5d71d1432f7c8151c58c4481",
"payment": {
"transactions": [
{
"_id": "5d71d1ff2f7c8151c58c44cf",
"method": "paytm",
"amount": 100,
"paymentOn": "2019-09-06T03:26:44.959Z"
}
],
"status": "partial"
},
"customer": "5d66c434c24f2b1fb6772014",
"order": {
"orderNumber": "WP-ORD-06092019-001",
"total": 200,
"balance": 100
}
}]
I want to aggregate payments by method.
So the result would look like below:
Output:
Paytm: 200
Cash : 650
Unpaid(Balance): 120
I have tried:
[
{
'$unwind': {
'path': '$payment.transactions',
'preserveNullAndEmptyArrays': true
}
}, {
'$project': {
'amount': '$payment.transactions.amount',
'method': '$payment.transactions.method'
}
}, {
'$group': {
'_id': '$method',
'amount': {
'$sum': '$amount'
}
}
}
]
But how to include balance calculation as well
Using the above dataset, use the aggregate pipeline for calculation using aggregate as:
db.collection.aggregate([
{
$facet: {
paidAmounts: [
{ '$unwind': { 'path': '$payment.transactions', 'preserveNullAndEmptyArrays': true } },
{
$group: {
_id: "$payment.transactions.method",
amount: {
$sum: "$payment.transactions.amount"
}
}
}
],
leftAmounts: [
{
$group: {
_id: null,
balance: {
$sum: "$order.balance"
}
}
}
]
}
}
])
giving output:
here leftAmounts has left balance and paidAmounts having grouped paid data on basis of payment type
[
{
"leftAmounts": [
{
"_id": null,
"balance": 120
}
],
"paidAmounts": [
{
"_id": "cash",
"amount": 650
},
{
"_id": "paytm",
"amount": 200
}
]
}
]
Working solution : https://mongoplayground.net/p/7IWELKKMsWe
db.collection.aggregate([
{
"$unwind": "$payment.transactions"
},
{
"$group": {
"_id": "$_id",
"balance": {
"$first": "$order.balance"
},
"paytm": {
"$sum": {
"$cond": [
{
"$eq": [
"$payment.transactions.method",
"paytm"
]
},
"$payment.transactions.amount",
0
]
}
},
"cash": {
"$sum": {
"$cond": [
{
"$eq": [
"$payment.transactions.method",
"cash"
]
},
"$payment.transactions.amount",
0
]
}
}
}
},
{
"$group": {
"_id": null,
"balance": {
"$sum": "$balance"
},
"cash": {
"$sum": "$cash"
},
"paytm": {
"$sum": "$paytm"
}
}
}
])