Inserting and Querying Date with MongoDB and Nodejs

后端 未结 4 920
春和景丽
春和景丽 2020-11-29 00:55

I need some help finding a record by date in mongodb and nodejs.

I add the date to the json object in a scraping script as follows:

jsonObj.last_upda         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 01:45

    I recently ran into the same issue and I wanted to add some additional info.

    You can insert dates in two ways:

    1. Created on the nodejs server side (e.g. in a ExpressJS script)

    example: insert a new object and time stamp it

    db.collection('mycollection').findOneAndUpdate({'name': req.body.name}, {
      // only "update" if it's an insert, meaning name is not found
      $setOnInsert: {
        'name': req.body.name,
        'date_added': new Date()
      }
    }, { 
      upsert: true
    }, (err, result) => {
      if(err) return res.send(err);
      res.send(result);
    });
    
    1. Created on the client side from JS and then sent over the wire using a REST API in a JSON object via a POST request

    example:

    // Send POST Request here
    fetch('addDate', {
      method: 'post',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({
        'date_added':  new Date()
      })
    })
    

    For 1. and 2. you create the dates using the new Date('2014-01-22T14:56:59.301Z') function as explained in the answers above. The difference is that if you do it as in 1. on the server side using the MongoClient package it is stored as a ISODate('2014-01-22T14:56:59.301Z') in MongoDB. If you do it as in 2. on the client side it is stored as a String: '2014-01-22T14:56:59.301Z' in MongoDB.

    To query the ISODate object you need to use new Date('2014-01-22T14:56:59.301Z') or ISODate('2014-01-22T14:56:59.301Z') To query the string you use just the string: '2014-01-22T14:56:59.301Z'

    Here are some query samples using the mongo shell:

    Date stored as String:

    db.mycollection.findOne({date_added {$gte:'2018-06-22T00:07:53.688Z'}})
    

    This returns

    {
      "_id" : ObjectId("5b2c3dd90078ce3cd484ef21"),
      "name" : "alfons",
      "date_added" : "2018-06-22T00:07:53.688Z"
    }
    

    Date stored as ISODate:

    db.mycollection.findOne({date_added: {$lte: ISODate('2018-06-26T23:24:37.023Z')}})
    

    or using "new Date" instead:

    db.mycollection.findOne({date_added: {$lte: new Date('2018-06-26T23:24:37.023Z')}
    

    This returns:

    {
      "_id" : ObjectId("5b2bb21e1954b104e61ff735"),
      "name" : "ceasar",
      "date_added" : ISODate("2018-06-21T14:11:41.323Z")
    }
    

提交回复
热议问题