如何在MongoDB(Mongoose)中更新和升级文档。的NodeJS

时间:2018-01-15 20:32:51

标签: javascript node.js mongodb mongoose

我有一个使用GET请求从服务器获取数据的服务。我每隔10秒重复一次这个请求,并在每次请求后将这些数据保存到我的数据库中。我的代码附在下面。 但我需要过滤我从服务器收到的新数据,以便不重复数据库中的数据。我读到我需要使用upset: true在我的数据库中进行更新,但我想我做错了。你能帮我完成这项任务吗? app.js代码:

const Tenders = require('./libs/mongoose');
const request = require('request');

let url = `http://public.api.openprocurement.org/api/2.4/tenders?offset=${new Date().toISOString()}+02.00`;

function getTenders() {
  request(url, { json: true }, (err, res, body) => {
    if (err) {
      return console.log(err);
    }
    url = `http://public.api.openprocurement.org/api/2.4/tenders?offset=${body.next_page.offset}`;

    const tendersList = [];
    let tendersData = new Tenders({ tenderId: String, tenderDate: String });

    body.data.forEach((item) => {
      tendersData = {
        tenderId: item.id,
        tenderDate: item.dateModified,
      };
      tendersList.push(tendersData);
    });

    Tenders.findAll({ tenderId: tendersData.tenderId }, (err, tenderId) => {
      if (!tenderId) {
        Tenders.insertMany(tendersList)
          .then((item) => {
            console.log('Saved to db');
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        console.log('Data is already in db');
      }
    });
  });
}

getTenders();
setInterval(getTenders, 10000);

以防mongoose.js:

const mongoose = require('mongoose');
const config = require('../config');

mongoose.Promise = global.Promise;
mongoose.connect(
  config.get('mongoose:uri'),
  { useMongoClient: true },
);

const tender = new mongoose.Schema({
  tenderId: String,
  tenderDate: String,
});

const Tenders = mongoose.model('Tenders', tender);

module.exports = Tenders;

我想我的代码Tenders.find....Tenders.insertMany只能看到一个项目而不是所有项目。那么,请你帮我插入我的第一部分数据,然后用数据库中的'upsert'数据与来自服务器的新数据一起?

1 个答案:

答案 0 :(得分:1)

使用upsert标志为true的更新函数可以简化插入/更新逻辑

这是一个实现,(希望tenderId被编入索引)

const TenderSchema = new Schema({ tenderId: String, tenderDate: String });
const Tender = mongoose.model('Tender', TenderSchema, 'tenders');

var tenders = [
    {tenderId :'tender-1', tenderDate : '1-1-2018'},
    {tenderId :'tender-2', tenderDate : '2-1-2018'},
    {tenderId :'tender-3', tenderDate : '3-1-2018'},
    {tenderId :'tender-2', tenderDate : '4-1-2018'},
    {tenderId :'tender-1', tenderDate : '5-1-2018'},
    {tenderId :'tender-2', tenderDate : '4-1-2018'}
];

for (var t of tenders){
    Tender.update(
        {'tenderId' : t.tenderId }, 
        {$set : t}, 
        {upsert : true, multi : true}, 
        function(err, doc){
                if(err) throw err;
                console.log(doc);
        }
    )
}

集合

> db.tenders.find()
{ "_id" : ObjectId("5a5d87d8a5f292efd566d186"), "tenderId" : "tender-1", "__v" : 0, "tenderDate" : "5-1-2018" }
{ "_id" : ObjectId("5a5d87d8a5f292efd566d187"), "tenderId" : "tender-2", "__v" : 0, "tenderDate" : "4-1-2018" }
{ "_id" : ObjectId("5a5d87d8a5f292efd566d188"), "tenderId" : "tender-3", "__v" : 0, "tenderDate" : "3-1-2018" }
> 

控制台日志

saravana@ubuntu:~/node-mongoose$ node so4.js
`open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or`createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mongo-client
Mongoose: tenders.update({ tenderId: 'tender-1' }, { '$set': { tenderId: 'tender-1', tenderDate: '1-1-2018' }, '$setOnInsert': { __v: 0 } }, { multi: true, upsert: true })
Mongoose: tenders.update({ tenderId: 'tender-2' }, { '$set': { tenderId: 'tender-2', tenderDate: '2-1-2018' }, '$setOnInsert': { __v: 0 } }, { multi: true, upsert: true })
Mongoose: tenders.update({ tenderId: 'tender-3' }, { '$set': { tenderId: 'tender-3', tenderDate: '3-1-2018' }, '$setOnInsert': { __v: 0 } }, { multi: true, upsert: true })
Mongoose: tenders.update({ tenderId: 'tender-2' }, { '$set': { tenderId: 'tender-2', tenderDate: '4-1-2018' }, '$setOnInsert': { __v: 0 } }, { multi: true, upsert: true })
Mongoose: tenders.update({ tenderId: 'tender-1' }, { '$set': { tenderId: 'tender-1', tenderDate: '5-1-2018' }, '$setOnInsert': { __v: 0 } }, { multi: true, upsert: true })
Mongoose: tenders.update({ tenderId: 'tender-2' }, { '$set': { tenderId: 'tender-2', tenderDate: '4-1-2018' }, '$setOnInsert': { __v: 0 } }, { multi: true, upsert: true })
{ n: 1, nModified: 1, ok: 1 }
{ n: 1, nModified: 1, ok: 1 }
{ n: 1, nModified: 0, ok: 1 }
{ n: 1, nModified: 1, ok: 1 }
{ n: 1, nModified: 1, ok: 1 }
{ n: 1, nModified: 0, ok: 1 }
^C
saravana@ubuntu:~/node-mongoose$