简体   繁体   English

检查字段是否存在mongodb node js

[英]check if field exists or not mongodb node js

I have a json data which is stored in mongodb. 我有一个存储在mongodb中的json数据。 so when i parse thru url using node how to check if the data or field that already exists in db if not insert it. 因此,当我使用节点解析URL时,如果不插入它,如何检查db中是否已存在数据或字段。 I am using mongodb node js driver to save data to db 我正在使用mongodb node js驱动程序将数据保存到db

example : 例如:

      {
       id :  "25"
       branch : "abc"
           {
             name : "xyz"
             email : 'xyz@abc.com'
            }
      }

so this is the json data i parse using node and store it in mongodb. 所以这是我使用node解析的json数据,并将其存储在mongodb中。 what my question when i get same data again how to check in database if the field already exists in db or else insert it as a new field in collection 当我再次获得相同的数据时,我的问题是什么?如何在数据库中检查该字段是否已存在,或者将其作为集合中的新字段插入

You can use a Unique Index ( http://docs.mongodb.org/manual/tutorial/create-a-unique-index/ ) to ensure that you don't insert the same data twice. 您可以使用唯一索引( http://docs.mongodb.org/manual/tutorial/create-a-unique-index/ )来确保不会两次插入相同的数据。

If you want to update a document if it exists or if not create it: Look at the save method ( http://docs.mongodb.org/manual/reference/method/db.collection.save/ ) 如果要更新文档(如果存在或不创建):查看保存方法( http://docs.mongodb.org/manual/reference/method/db.collection.save/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM