简体   繁体   English

MongoDB和Mongoose:如何添加对象(如果尚不存在)

[英]MongoDB and mongoose: how to add an object if it doesn't already exist

I am having some trouble with mongoDB/mongoose and node.js. 我在使用mongoDB / mongoose和node.js时遇到了一些麻烦。 I am used to SQL, and mongoDB is...hard! 我已经习惯了SQL,而mongoDB很难...! Here is my schema: 这是我的架构:

var mongoose = require('mongoose'); var mongoose = require('mongoose');

mongoose.Promise = global.Promise; mongoose.Promise = global.Promise;

var itemSchema= mongoose.Schema({

    item_info        : {
        user_id      : Number,
        location     : String,
        item_id      : Number,
        title        : String
    },
    item_hist        : {
        user_id      : Number,
        location     : String,
        item_id      : Number,
        founddate    : String
       }
});
module.exports = mongoose.model('item', itemSchema);

And I can add a new item by doing this: 我可以通过执行以下操作添加一个新项目:

    var item= require('./app/models/item');
    var item= new item();
    item.item_info.user_id      =  12345;
    item.item_info.location     = 'This address';
    item.item_info.item_id      = 4444;
    item.item_info.title        = 'New item';
    item.save(function(err) 
    {
        if (err)    throw err;
    });

What I want to be able to do is say: "look for an item with item_info.item_id 5555. if it exists, do nothing. if it doesn't exist, then add it to the database." 想做的是说:“寻找一个具有item_info.item_id 5555的项目。如果存在,则什么也不做。如果不存在,则将其添加到数据库中。” I've read through so much mongodb and mongoose documentation, but between using dot notation and accessing through nodejs instead of command line mongodb, I still can't figure out how to do this. 我已经阅读了很多mongodb和mongoose文档,但是在使用点表示法和通过nodejs而不是命令行mongodb访问之间,我仍然不知道该怎么做。 SQL seemed so much easier! SQL似乎容易得多!

Just use this - 只需使用-

    var query = { user_id: 12345, location: "This address", item_id: 4444, title: "New item"  },

        options = { upsert: true };

   Model.findOneAndUpdate(query.item_id, query, options, function(error, result) {
    if (error) return;

    // do something with the document
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 复杂的猫鼬从api中提取数据列表,如果还不存在,则插入到mongodb中 - complicated mongoose pull list of data from api and insert into mongodb if it doesn't already exist 如果项目不存在,则将项目添加到数组 mongodb - add item to array if it doesn't exist mongodb MongoDB 和 API 集成 - 如果不存在则创建新用户 - MongoDB and API integration - Create New User if Doesn't Already Exist Mongoose - 如何确定查询中是否已存在嵌套对象? - Mongoose - how to determine whether a nested object already exist in a query? 在 Mongoose 的模型对象上调用 save() 不会将记录保存到 MongoDB - Calling save() on the object of a model in Mongoose doesn't save the record to MongoDB 如果文档存在,如何更新文档,如果它在 mongodb 中不存在,则如何插入? - How to update document if it exist and insert it if it doesn't exist in mongodb? 如何在 MongoDB 中使用 ZCADCDEDB5097ABAE643E15DCEDF 将新的 Object 添加到特定 Object 的数组中 - How to add a new Object into an Array of a particular Object in MongoDB using Mongoose? 如何从MongoDB中选择某些字段并返回空对象或字符串(如果不存在)? - How do I select certain fields from a MongoDB and return an empty object or string if it doesn't exist? node js,mongodb:检查对象是否已存在 - node js , mongodb : check if an object already exist 如果 postgresql POOL 查询不存在,如何插入用户名 - How to insert a username if it doesn't already exist with a postgresql POOL query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM