许多插入mongoose,如果存在检查数据后如何插入?

时间:2017-11-28 19:00:57

标签: node.js mongodb

大家好我正在研究节点js为初学者从expres制作api,我想问一下如何在存在数据后插入很多数据并在数据不存在时创建数据? 例如 所以我在数据库中有数据json

[{
   "_id":1,
   "product": t-shirt,
   "brand": polo,
   "price": ["3.335.000","4.000.000"],
   "detail": {
      "desc": "test1",
      "size": "L"
    }
 },
{
   "_id":2,
   "product": t-shirt,
   "brand": adidas,
   "price": ["2.335.000","3.000.000"],
   "detail": {
      "desc": "test2",
      "size": "L"
    }
 },{
   "_id":3,
   "product": t-shirt,
   "brand": puma,
   "price": ["5.000.000","9.000.000"],
   "detail": {
      "desc": "test3",
      "size": "XL"
    }
 }]

我有数据,用于post / insert to db

[{
       "product": t-shirt,
       "brand": polo,
       "price": ["3.335.000","4.000.000"],
       "detail": {
          "desc": "test1",
          "size": "L"
        }
     },
    {
       "product": t-shirt,
       "brand": adidas,
       "price": ["2.335.000","3.000.000"],
       "detail": {
          "desc": "test2",
          "size": "L"
        }
     },{
       "product": t-shirt,
       "brand": puma,
       "price": ["5.000.000","9.000.000"],
       "detail": {
          "desc": "test3",
          "size": "XL"
        }
     },
    {
       "product": t-shirt,
       "brand": nike,
       "price": ["5.000.000","9.000.000"],
       "detail": {
          "desc": "test4",
          "size": "XL"
        }
     },
    {
       "product": t-shirt,
       "brand": aon,
       "price": ["5.000.000","9.000.000"],
       "detail": {
          "desc": "test5",
          "size": "XL"
        }
     }
    ]

更新

这是来自服务器的代码

var Gnew        = require('../models/global/Globals.Prod.js');
var fs          = require('fs');

module.exports = function(prouds) { 

//CREATE
prouds.post('/create', function(req, res, err){
  Gnew.find({}, function(err, proudcts){
    if (err){
      console.log(err)
    }else{
      // IF PRODUCTS === REQ.BODY THEN CREATE REQ.BODY NOT EXIST IN PRODUCT
      // HOW TO CREATE THIS?
    }
  })
return prouds;
};

我使用邮递员发布数据,发布后我从数据库获取检查数据的所有数据,我想如果存在,他只是忽略相同的数据,我想要发布数据品牌nike and aon,因为品牌为{{1以及如何创建编码?

oke我试试这样的条件,我只是想让数据不存在于db中,为什么输出与输入一样?这段代码有什么问题?

puma, adidas, and polo is exist

谢谢

0 个答案:

没有答案