【问题标题】:MongoDB + nodejs : how to query ISODate fields?MongoDB + nodejs:如何查询 ISODate 字段?
【发布时间】:2018-01-18 13:14:53
【问题描述】:

我正在使用 nodejs 和 node-mongodb-native 驱动程序 (http://mongodb.github.io/node-mongodb-native/)。

我有一个日期属性存储为ISODate 类型的文档。

通过nodejs,我正在使用这个查询:

db.collection("log").find({
    localHitDate: { 
            '$gte': '2013-12-12T16:00:00.000Z',
            '$lt': '2013-12-12T18:00:00.000Z' 
    }
})

它什么也不返回。为了使其工作,我需要执行以下操作:

db.collection("log").find({
    localHitDate: {
            '$gte': ISODate('2013-12-12T16:00:00.000Z'),
            '$lt': ISODate('2013-12-12T18:00:00.000Z')
    }
})

ISODate 在我的 nodejs 代码中无法识别。

那么如何通过我的 nodejs 程序对 mongo 日期字段进行查询?

谢谢

【问题讨论】:

    标签: node.js mongodb isodate


    【解决方案1】:

    您可以浏览我在其他问题中提供的答案。

    他们而不是 $eq 使用 $gte:"yyyy-mm-dd", $lte:"yyyy-mm-dd"

    NodeJS MongoDB: Querying ISO Date

    希望这会对您或其他人有所帮助!

    【讨论】:

      【解决方案2】:

      我们需要使用 new Date() 是获取数据的最佳选择。

      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"
            }
          }
        }
      ])
      

      【讨论】:

        【解决方案3】:

        你可以用这个,对我来说效果很好

        //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){ 
        
                }
        
              }
            });
        
        
          }
        
        });
        

        【讨论】:

          【解决方案4】:

          你可以在node.js中使用new Date('2013-12-12T16:00:00.000Z')

          new 是必须的,因为 Date() 已经用于返回日期字符串。

          ISODate是mongodb中的概念,你可以在mongodb控制台中使用它,但是对于不同的编程语言它可以是不同的。

          【讨论】:

          • 我真的遇到了非常困难的时间...我似乎无法在 mongo 中存储 utc isodate 来挽救我的生命...stackoverflow.com/questions/26874993/…
          • 感谢您的回答。节省了很多时间
          • 12 是一个月还是一天?
          猜你喜欢
          • 1970-01-01
          • 2015-10-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-28
          • 1970-01-01
          相关资源
          最近更新 更多