Mongoose $or query with $near doesn't seem to work - node.js

I'm trying to run an $or query but whenever I use $near in one of the $or clauses, the query returns 0 results.
Here is my code:
params = { $or: [
{ loc: { '$near': {
'$geometry': {
type : "Point" ,
coordinates : [ -0.127500 , 51.507220 ]
},
'$maxDistance': 1000
} } },
{ categories: { $in: ['Services'] } }
] };
var query = Activity.find(params).populate({path: '_place'});
query.limit(10).exec('find', function(err, items) {
...
});

Related

Unknown $or operator in MongoDB Realm

I'm trying to do a simple or query for mongoDB Realm, but I do get the error MongoError: unknown operator: $or
Could someone explain what I am doing wrong?
exports = function(arg){ let collection = context.services.get("mongodb-atlas").db("M_and_H").collection("M_and_H");result = collection.aggregate([
{
'$match': {
'prod_name': {
'$regex': 'T-shirt',
'$or': [
{
'colour_group_name': 'Red'
}, {
'colour_group_name': 'Orange'
}
]
}
}
}]) return result;};

mongo Queries With Multiple Expressions Specifying the Same Operator

Hello I am trying to perform the following query in my database
function getChat(nameChat, nameUser, callback) {
global.db.collection('chatUsers').find({
$and: [
{ $or : [ { nameChat: nameChat }, { nameChat: nameUser } ] },
{ $or : [ { nameUser: nameChat }, { nameUser: nameUser } ] }
]
}).sort({date: 1}).toArray(
function (err, docs) {
if (err) return console.log("algo deu errado" + err);
console.log("exit\n"+docs+nameUser);
callback(docs);
}
);}
But the query returns empty Here is an example of the objects I have in my chat Users collection:
{"_id":"5bc3e6d060d4da04dcd66517","nameChat":"Daciolo bolsominion","nameUser":"Mc Xhamps","date":"2018-10-14T17:22:00.000Z","msg":"VocĂȘ pertence a URSAL"}
{"_id":"5bc52fc3b878631f84c77c41","nameChat":"Mc Xhamps","nameUser":"Daciolo bolsominion","date":"2018-10-01T17:25:00.000Z","msg":"E ai, bora ser Presidente"}
To be clear I would like my query to return all objects that name Chat or name User are present in the same object
This query with this is only returning empty objects.
Hey Erik change the and with or you will get required result.
global.db.collection('chatUsers').find({
$or: [
{ $or : [ { nameChat: nameChat }, { nameChat: nameUser } ] },
{ $or : [ { nameUser: nameChat }, { nameUser: nameUser } ] }
]
}).sort({date: 1})

Mongodb aggregation does't work with $match between times

Here is my code
Bill.aggregate(
{$unwind:'$detais'},
{ $match : {
createdOn : {
$gt : moment().startOf('day'),
$lt : moment().endOf('day')
}
}
},
{
$group : {
_id : '$detais.product_id',
total : { $sum : '$detais.quantity' },
}
},
{ $sort :{ total: -1 } }
)
.limit(10)
.exec((err, records) => {
if (err) {console.log(err)};
res.send({
data : records
});
});
The query
createdOn : {
$gt : moment().startOf('day'),
$lt : moment().endOf('day')
}
work fine in another case.
But in aggregate is empty result... Please someone tell me where i'm mistake....
You need to apply $and condition in $match.
Bill.aggregate({
$unwind: '$detais'
}, {
$match: {
createdOn: {
$and: [{
$gt: moment().startOf('day')
},
{
$lt: moment().endOf('day')
}]
}
});

NodeJs : $nin array query in $geoNear in MongoDb doesn't work

I don't understand why my query doesn't work with MongoDb $geoNear ?
I checked this issue without success : mongodb geoNear command with filter
My environment :
NodeJs : V6.6.0
Mongoose : 4.8.6
this works
db.collection.geoNear(
coords,
{
query : { status: "1" } }, // I have the good filtered results
...
I already tested
...
query : { _id: { $in: itemIds } }, //no error and empty result
query : { "_id": "5639ce8c01f7d527089d2a74" }, //no error and empty result
query : { "_id" : 'ObjectId("5649e9f35f0b360300cad022")' }, //no error and empty result
query : { "_id" : ObjectId("5649e9f35f0b360300cad022") }, //error ObjectId not defined
...
I want this
db.collection.geoNear(
coords,
{
query : { _id: { $nin: poiIds } }, // no error but I have not the good results because I obtain all results geolocalized
...
Thank you ;-)
For this issue:
query : { "_id" : ObjectId("5649e9f35f0b360300cad022") }, //error ObjectId not defined
Try:
const ObjectID = require('mongodb').ObjectID
In my case, I did it like this:
Add index to coords in your model, then request with query.
modelSchema.index({ coords: '2dsphere' });
query = {
_id: {
$nin: poiIds
},
coords: {
$near: {
$geometry: { type: "Point", coordinates: position }
}
}
}
where position is an array [Lat, Lon].
Hope this helps. ;)

Is it possible that mongodb's $project return an array?

Is it possible for MongoDb's $project aggregation operator to restructure the document to an array ?
Here is what I did so far:
var pipeline = [];
var project = {
$project : {
x: "$_id",
y: "$y" ,
_id : 0
}
};
pipeline.push(project);
model.aggregate( pipeline, callback);
This gives me output of form:
[
{
x: '...',
y: '...'
}
....
]
I would like to have:
[
['..','..']
....
]
I can easily restructure the output by iterating it, but really curious to know if aggregate itself could return array instead of object.
You could try with the $push operator.
For example,
if you had documents like:
{ _id: <something>, y: 5 }
In the mongo shell, if you type
db.model.aggregate( [ { $group: { _id: null, newArrayField: { $push: { x: "$_id", y: "$y" } } } } ] )
You would get:
{
"result" : [
{
"_id" : null,
"newArrayField" : [
{
"x" : ObjectId("5265dd479eb4b1d4289cf222"),
"y" : 5
}
]
}
],
"ok" : 1
}
For more information on $push operator, see http://docs.mongodb.org/manual/reference/operator/aggregation/push/
With MongoDB 3.2 you can project array values
db.model.aggregate({
$project: {arrayField: ['$_id', '$y']}
})

Resources