MongoDB + nodejs : how to query ISODate fields?

后端 未结 4 1843
失恋的感觉
失恋的感觉 2020-12-01 11:58

I am using nodejs with the node-mongodb-native driver (http://mongodb.github.io/node-mongodb-native/).

I have documents with a date property stored as ISODate<

相关标签:
4条回答
  • 2020-12-01 12:21

    we need to use new Date() is best option to get the data.

    db.getCollection('orders').aggregate([
      {
        '$match': {
          $and: [
            {
              status: 'UNASSIGNED'
            },
            {
              plannedDeliveryDate: {
                '$eq': new Date('2017-10-09')
              }
            }
          ]
        }
      },
      {
        $lookup: {
          from: "servicelocations",
          localField: "serviceLocationId",
          foreignField: "serviceLocationId",
          as: "locations"
        }
      },
      {
        $unwind: "$locations"
      },
      {
        "$project": {
          "accountId": 1,
          "orderId": 1,
          "serviceLocationId": 1,
          "orderDate": 1,
          "description": 1,
          "serviceType": 1,
          "orderSource": 1,
          "takenBy": 1,
          "plannedDeliveryDate": 1,
          "plannedDeliveryTime": 1,
          "actualDeliveryDate": 1,
          "actualDeliveryTime": 1,
          "deliveredBy": 1,
          "size1": 1,
          "size2": 1,
          "size3": 1,
          "jobPriority": 1,
          "cancelReason": 1,
          "cancelDate": 1,
          "cancelBy": 1,
          "reasonCode": 1,
          "reasonText": 1,
          "status": 1,
          "lineItems": 1,
          "locations": {
            "lng": "$locations.location.lng",
            "lat": "$locations.location.lat"
          }
        }
      }
    ])
    
    0 讨论(0)
  • 2020-12-01 12:24

    You can use this, for me worked perfectly

    //lets require/import the mongodb native drivers.
    var mongodb = require('mongodb');
    
    //We need to work with "MongoClient" interface in order to connect to a mongodb server.
    var MongoClient = mongodb.MongoClient;
    
    // Connection URL. This is where your mongodb server is running.
    var url = 'mongodb://localhost/klevin';
    
    // Use connect method to connect to the Server
    MongoClient.connect(url, function (err, db) {
    
      if (err) {
        console.log('Unable to connect to the mongoDB server. Error:', err);
      } else {
        //HURRAY!! We are connected. :)
        console.log('Connection established to', url);
    
    
        // Get the documents collection
        var collection = db.collection('frames');
    
        //We have a cursor now with our find criteria
        var cursor = collection.find({
          tv: 'tematv', 
          date_created: {"$gte": new Date("2015-10-01T00:00:00.000Z") , "$lt": new Date("2017-03-13T16:17:36.470Z") }});
    
        //We need to sort by age descending
        cursor.sort({_id: -1});
    
        //Limit to max 10 records
        cursor.limit(50);
    
        //Skip specified records. 0 for skipping 0 records.
        cursor.skip(0);
    
    
        //Lets iterate on the result
        cursor.each(function (err, doc) {
    
          if (err) {
    
            console.log(err);
    
          } else {
    
            console.log('Fetched:', doc);
    
            if(doc !== null){ 
    
            }
    
          }
        });
    
    
      }
    
    });
    
    0 讨论(0)
  • 2020-12-01 12:24

    You can go through my answer, provided in other asked question.

    Their instead of $eq use $gte:"yyyy-mm-dd", $lte:"yyyy-mm-dd"

    NodeJS MongoDB: Querying ISO Date

    Hope this will help you or somebody else!

    0 讨论(0)
  • 2020-12-01 12:25

    You can use new Date('2013-12-12T16:00:00.000Z') in node.js;

    new is a must, because Date() is already use to return date string.

    ISODate is concepted in mongodb, you can use it in mongodb console, but it can be different for different programming language.

    0 讨论(0)
提交回复
热议问题