Time range $and bad query?

I am using $and operator to get query to items instantaneously (+/- 1 from present query) so filtering out old and future items already in collection, why am I getting $and as bad query?

{ [MongoError: exception: bad query: BadValue unknown operator: $and]
  name: 'MongoError',
  errmsg: 'exception: bad query: BadValue unknown operator: $and',
  code: 16810,
  ok: 0 }

async function getAggregateSparkUserIdCountUp(details) { // userid -> 'abc123'

var promise = await new Promise(function (resolve, reject) {
    FutureTasks.aggregate([
        {
            $match: {
                "userid": {$eq: details.userid},

                "start_date": {
                     $and: [
                        {$lte: moment(details.start_date).local().subtract(1, 'minutes').format("YYYY-MM-DD HH:mm:ss"),
                         $gte: moment(details.start_date).local().add(1, 'minutes').format("YYYY-MM-DD HH:mm:ss")}
                     ]
                }
            }
        },


        {
            $group: {
                _id: "$userid", count: {$sum: 1}
            }
        }
    ], (err, db) => {
        err ? reject(err) : resolve(db);
    });
}); // End of promise
return promise; // promise -> [ { _id: 'abc123', count: 1 } ] or []

}


https://docs.mongodb.com/manual/reference/operator/aggregation/and/

Is it due to not using $project keyword?

$and is nested a bit too deeply here I believe, so if you were to adjust your $match selector to be more along the lines of

{userId: details.userid, $and: [
    {start_date: {$lte: moment(details.start_date).local().subtract(1, 'minutes').toDate()}},
    {start_date: {$gte: moment(details.start_date).local().add(1, 'minutes').toDate()}}
]}

it’d probably do the trick.

does this need to be a rawCollections to work?

async function getAggregateSparkUserIdCountUp(details) {

var promise = await new Promise(function (resolve, reject) {


    FutureTasks.aggregate(
        [
            {
                $match: {
                    "userid": {$eq: details.userid},

                    $and: [
                        {"start_date": {$lte: moment(details.start_date).local().subtract(1, 'minutes').format("YYYY-MM-DD HH:mm:ss")}},
                        {"start_date": {$gte: moment(details.start_date).local().add(1, 'minutes').format("YYYY-MM-DD HH:mm:ss")}}
                    ]

                }
            },
            



            {
                $group: {
                    _id: "$userid", count: {$sum: 1}
                }
            }
        ]
        , (err, db) => {
            err ? reject(err) : resolve(db);
        });
});  
return promise; 
}

Aggregate is not exposed on meteor’s mongo driver, so rawCollection() would be the place to hook into it. There’s an atmosphere package which does this, or you can wrap it yourself like something along the lines of:

Mongo.Collection.prototype.rawFindOne = function(selector, options) {
    options = options || {};
    var coll = this.rawCollection();
    return q.denodeify(coll.findOne.bind(coll))(selector, options);
};

which gives you more flexibility.

Hi mds4m

But I have used aggregate and returning the result I expected…?
so not sure why you said aggregate not available?

async function getAggregateSparkUserIdCount(userid) {  

var promise = await new Promise(function (resolve, reject) {
    FutureTasks.aggregate([
    
        {
            $match: {
                "userid": {$eq: userid},

                $and: [
                    {start_date: {$lte: moment().local().add(1, 'minutes').format("YYYY-MM-DD HH:mm:ss")}},
                    {start_date: {$gte: moment().local().subtract(1, 'minutes').format("YYYY-MM-DD HH:mm:ss")}}
                ]
            }
        },

        {
            $group: {
                _id: "$userid", count: {$sum: 1}
            }
        }
    ], (err, db) => {
        err ? reject(err) : resolve(db);
    });
}); // End of promise
return promise;

} /

I believe aggregate isn’t implemented on the Meteor mongo driver. But if the method is present on your collection object then perhaps you’ve already added it to the Mongo.Collection prototype or installed a package which does this. It sounds like a moot issue if it works, I was just addressing your previous question.