Anton Putau
Anton Putau

Reputation: 772

find method does not work with dates

I am accessing documentdb via mongoose driver from nodejs.

db.recordingModel.find(
    {
        dateRecorded :
            {
                "$gte": new Date("2015-10-01T00:00:00.000Z")
            }

        // status:"sync_error"

    }, function(err, results) {
        console.log(results);
        console.log(err);
    });

I got next error -

{ [MongoError: cursor does not exist, was killed or timed out]
  name: 'MongoError',
  message: 'cursor does not exist, was killed or timed out' }

If I access any other field all works ok. What's wrong with this query ? Thanks.

Workaround

I have noticed from azure portal that date field has complex structure, so I update my query.

"dateRecorded.$date" :
 {
     "$gte": 1491956026000
 }

Upvotes: 0

Views: 288

Answers (1)

Aaron Chen
Aaron Chen

Reputation: 9950

I am not able to reproduce your issue on my site with Mongoose v4.7.1.

Here is my full sample code:

var mongoose = require('mongoose');
mongoose.connect('DocumentDB-connection-string');

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  // we're connected!

  var kittySchema = mongoose.Schema({
    name: String,
    createdAt: Date
  });

  var Kitten = mongoose.model('Kitten', kittySchema);

  var fluffy = new Kitten({ name: 'fluffy', createdAt: new Date() });

  fluffy.save(function (err, fluffy) {
    if (err) return console.error(err);

    Kitten.find({createdAt: { '$gte': new Date("2015-10-01T00:00:00.000Z") }}, function (err, kittens) {
      if (err) return console.error(err);
      console.log(kittens);
    })

  });

});

My documents:

enter image description here

The output:

enter image description here

Upvotes: 1

Related Questions