In MongoDB, how to filter by location, sort by another field and paginate the results correctly? - mongodb

When i don't use pagination, everything works fine (i have only 3 records in this collection, so all of them are listed here):
db.suppliers.find({location: {$near: [-23.5968323, -46.6782386]}},{name:1,badge:1}).sort({badge:-1})
{ "_id" : ObjectId("4f33ff549112b9b84f000070"), "badge" : 3, "name" : "Dedetizadora Alvorada" }
{ "_id" : ObjectId("4f33ff019112b9b84f00005b"), "badge" : 2, "name" : "Sampex Desentupidora e Dedetizadora" }
{ "_id" : ObjectId("4f33feae9112b9b84f000046"), "badge" : 1, "name" : "Higitec Desentupimento e Dedetização" }
But when i try to paginate from the first to the second page, one record doesn't show up and one is repeated:
db.suppliers.find({location: {$near: [-23.5968323, -46.6782386]}},{name:1,badge:1}).sort({badge:-1}).skip(0).limit(2)
{ "_id" : ObjectId("4f33ff549112b9b84f000070"), "badge" : 3, "name" : "Dedetizadora Alvorada" }
{ "_id" : ObjectId("4f33feae9112b9b84f000046"), "badge" : 1, "name" : "Higitec Desentupimento e Dedetização" }
db.suppliers.find({location: {$near: [-23.5968323, -46.6782386]}},{name:1,badge:1}).sort({badge:-1}).skip(2).limit(2)
{ "_id" : ObjectId("4f33feae9112b9b84f000046"), "badge" : 1, "name" : "Higitec Desentupimento e Dedetização" }
Am i doing something wrong or is this some kind of bug?
edit:
Here is a workaround for this. Basically you shouldn't mix $near queries with sorting; use $within instead.

There is an open issue regarding the same problem. Please have a look & vote Geospatial result paging fails when sorting with additional keys

Related

how to calculate time difference based on 2 fields in mongodb

I am familiar to simple mongodb queries but this one is a bit complex for me. Here, what I am trying to achieve is on the basis of jsonObject.callID and jsonObject.mobile fields I have to calculate time difference of jsonObject.timestamp. For example in below sample documents, jsonObject.callID and mobile will remain same for jsonObject.action start and end. So based on jsonObject.callID and jsonObject.mobile, I have to subtract the jsonObject.timestamp. jsonObject.callId will be same for two interval actions i.e. start and end with their same jsonObject.mobile numbers.
{
"_id" : ObjectId("5df9bc5ee5e7251030535df5"),
"_class" : "com.abc.mongo.docs.IvrMongoLog",
"jsonObject" : {
"mode" : "ivr",
"callID" : "33333",
"callee" : "128",
"action" : "end",
"mobile" : "218924535466",
"timestamp" : "2019-12-18 16:18:12"
}
}
{
"_id" : ObjectId("5df9bc3de5e7251030535df4"),
"_class" : "com.abc.mongo.docs.IvrMongoLog",
"jsonObject" : {
"mode" : "ivr",
"callID" : "33333",
"callee" : "128",
"action" : "start",
"mobile" : "218924535466",
"timestamp" : "2019-12-18 16:12:11"
}
}
So I am trying to achieve a output like below:
{
"callee" : "128",
"mobile" : "218924535466",
"callID" : "33333",
"minutes_of_call" : "6" // difference of "2019-12-18 16:18:12" - "2019-12-18 16:12:11"
}
subsequently I need such results for next documents...
Kindly assist.

$nin operator doesn't work correctly

When I use this method, I get all documents from _User collection. (as expected)
db.getCollection("_User").find({ "_id" : { $nin: [] }})
However, when I add at least one string to $nin array:
db.getCollection("_User").find({ "_id" : { $nin: ["7HpHbmF5iu","ga61t3afsa"] }})
I get empty results. (expected - to get all documents without the ones with ids 7HpHbmF5iu and ga61t3afsa)
MongoDB version 3.2.6
These are two documents I want to filter from all:
{ "_id" : "7HpHbmF5iu", "expiration_date" : ISODate("2015-12-28T08:51:21.252Z"), "_created_at" : ISODate("2015-10-29T08:51:23.302Z"), "_updated_at" : ISODate("2016-03-10T17:37:45.262Z"), "gender" : "female", "firstName" : "Toma", "verification" : "verified", "birthdayDate" : ISODate("1995-02-15T00:00:00Z") }
{ "_id" : "ga61t3afsa", "expiration_date" : ISODate("2015-12-27T07:54:21.235Z"), "_created_at" : ISODate("2015-10-28T07:11:22.102Z"), "_updated_at" : ISODate("2016-03-11T16:11:10.100Z"), "gender" : "female", "firstName" : "Goda", "verification" : "verified", "birthdayDate" : ISODate("1992-09-13T00:00:00Z") }
It was an error in Azure DocumentDB with Mongo, however I've chosen not to use their services, so I don't know if they've already fixed it.

MongoDB .Net driver 2.0 Builders Filter (field to array comparison)

I need to get all usernames from "followingList.username" and compare with
posts' usernames, if there any match need to add that one to an array.
Person Model
{
"_id" : ObjectId("554f20f5c90d3c7ed42303e1"),
"username" : "fatihyildizhan",
"followingList" : [
{
"_id" : ObjectId("55505b6ca515860cbcf7901d"),
"username" : "gumusluk",
"avatar" : "avatar.png"
},
{
"_id" : ObjectId("58505b6ca515860cbcf7901d"),
"username" : "yalikavak",
"avatar" : "avatar.png"
},
{
"_id" : ObjectId("58305b6ca515860cbcf7901d"),
"username" : "gumbet",
"avatar" : "avatar.png"
}
]
}
Post Model
{
"_id" : ObjectId("554f2df2a388R4b425b89833"),
"username" : "yalikavak",
"category" : "Summer",
"text" : "blue voyage with yacht"
},
{
"_id" : ObjectId("554f2df2a388P4b425b89833"),
"username" : "yalikavak",
"category" : "Winter",
"text" : "is coming ..."
},
{
"_id" : ObjectId("554f2df2a388K4b425b89833"),
"username" : "gumbet",
"category" : "Fall",
"text" : "there are many trees"
}
I try to get result code block as below but couldn't figure it out.
var filter = Builders<Post>.Filter.AnyEq("username", usernameList);
var result = collection.Find(filter).ToListAsync().Result;
Can you help me with this?
Thanks.
The logic is flipped, what you need is an $in query if I understand your use case correctly:
var filter = Builders<Post>.Filter.In("username", usernameList);
var result = collection.Find(filter).ToListAsync().Result;
In your case, username is a simple field and you want to match against a list of candidates. AnyEq is used to check that, from an embedded list of complex objects, at least one matches a criterion. That still translates to a simple query in MongoDB, but requires to 'reach into' the object which needs a more complicated syntax.

Get specific object in array of array in MongoDB

I need get a specific object in array of array in MongoDB.
I need get only the task object = [_id = ObjectId("543429a2cb38b1d83c3ff2c2")].
My document (projects):
{
"_id" : ObjectId("543428c2cb38b1d83c3ff2bd"),
"name" : "new project",
"author" : ObjectId("5424ac37eb0ea85d4c921f8b"),
"members" : [
ObjectId("5424ac37eb0ea85d4c921f8b")
],
"US" : [
{
"_id" : ObjectId("5434297fcb38b1d83c3ff2c0"),
"name" : "Test Story",
"author" : ObjectId("5424ac37eb0ea85d4c921f8b"),
"tasks" : [
{
"_id" : ObjectId("54342987cb38b1d83c3ff2c1"),
"name" : "teste3",
"author" : ObjectId("5424ac37eb0ea85d4c921f8b")
},
{
"_id" : ObjectId("543429a2cb38b1d83c3ff2c2"),
"name" : "jklasdfa_XXX",
"author" : ObjectId("5424ac37eb0ea85d4c921f8b")
}
]
}
]
}
Result expected:
{
"_id" : ObjectId("543429a2cb38b1d83c3ff2c2"),
"name" : "jklasdfa_XXX",
"author" : ObjectId("5424ac37eb0ea85d4c921f8b")
}
But i not getting it.
I still testing with no success:
db.projects.find({
"US.tasks._id" : ObjectId("543429a2cb38b1d83c3ff2c2")
}, { "US.tasks.$" : 1 })
I tryed with $elemMatch too, but return nothing.
db.projects.find({
"US" : {
"tasks" : {
$elemMatch : {
"_id" : ObjectId("543429a2cb38b1d83c3ff2c2")
}
}
}
})
Can i get ONLY my result expected using find()? If not, what and how use?
Thanks!
You will need an aggregation for that:
db.projects.aggregate([{$unwind:"$US"},
{$unwind:"$US.tasks"},
{$match:{"US.tasks._id":ObjectId("543429a2cb38b1d83c3ff2c2")}},
{$project:{_id:0,"task":"$US.tasks"}}])
should return
{ task : {
"_id" : ObjectId("543429a2cb38b1d83c3ff2c2"),
"name" : "jklasdfa_XXX",
"author" : ObjectId("5424ac37eb0ea85d4c921f8b")
}
Explanation:
$unwind creates a new (virtual) document for each array element
$match is the query part of your find
$project is similar as to project part in find i.e. it specifies the fields you want to get in the results
You might want to add a second $match before the $unwind if you know the document you are searching (look at performance metrics).
Edit: added a second $unwind since US is an array.
Don't know what you are doing (so realy can't tell and just sugesting) but you might want to examine if your schema (and mongodb) is ideal for your task because the document looks just like denormalized relational data probably a relational database would be better for you.

How to run multi condition check in mongodb update query?

I have the following scenario.
I need to check multiple condition in a single update query.
Sample Data :
"host" : "zigwheels.com",
"lastAccessDate" : "20140819",
"sessionId" : "ff8378ed-ccda-4a75-b24b-4a4bb1153e39"
Here is my requirement :
obj=db.userFrequency.find({"host" : "xyz.com", "sessionId" : "ff8378ed-ccda-4a75-b24b-4a4bb1153e39"});
if(obj!=null){
if(obj.get("lastAccessDate")!= todayDate){
//If page has not visited today, increment count by 1 and update "lastAccessDate"=todayDate
db.userFrequency.update({"host" : "xyz.com","sessionId" : "ff8378ed-ccda-4a75-b24b-4a4bb1153e39"},{$set : {"lastAccessDate"=todayDate},$inc : {"count":1}});
}
else{
// If page visited today{lastAccessDate==todayDate}, no need to update count
}
}else{
//Insert the new Entry
db.userFrequency.update({"host" : "xyz.com","sessionId" : "ff8378ed-ccda-4a75-b24b-4a4bb1153e39"},{$set : {"lastAccessDate"=todayDate},$inc : {"count":1}},{upsert:true});
}
I need to do the above operation in a single query but I have GB's of data.
I tried above like below :
db.userFrequency.update({"host" : "xyz.com","lastAccessDate"!=todayDate,"sessionId" : "ff8378ed-ccda-4a75-b24b-4a4bb1153e39"},{$set : {"lastAccessDate"=todayDate},$inc : {"count":1}});
but it inserts new entry because the above condition requires three checks.
Please suggest a solution for the same.
It's awkward if you store your date as a string. Store dates as date type fields and this is easy:
> db.userFrequency.insert({
"host" : "zigwheels.com",
"lastAccessDate" : ISODate("2014-08-19"),
"sessionId" : "ff8378ed-ccda-4a75-b24b-4a4bb1153e39"
})
> var today = ISODate("2014-08-25")
> db.userFrequency.update({
"host" : "zigwheels.com",
"lastAccessDate" : { "$lt" : today },
"sessionId" : "ff8378ed-ccda-4a75-b24b-4a4bb1153e39"
},
{
$set : { "lastAccessDate" : today},
$inc : { "count" : 1 }
})
> db.userFrequency.findOne()
{
"_id" : ObjectId("53fbd08...398"),
"host" : "zigwheels.com",
"lastAccessDate" : ISODate("2014-08-25T00:00:00Z"),
"sessionId" : "ff8378ed-ccda-4a75-b24b-4a4bb1153e39",
"count" : 1
}
I used the $lt operator for the date test because users won't access the website from the future...hopefully. Using proper dates also surfaces another important issue- what timezone defines today? Right now, for me, it's tomorrow in Australia...