mongodb插入数据

##################################

 

一、插入数据insert :

插入一条数据:

db.products.insert( { _id: 10, item: "box", qty: 20 } )

 

插入多条数据:

db.products.insert(
   [
     { _id: 11, item: "pencil", qty: 50, type: "no.2" },
     { item: "pen", qty: 20 },
     { item: "eraser", qty: 25 }
   ]
)

 

 

 

 

 

二、一次插入一条数据insertOne :

 

db.products.insertOne( { item: "card", qty: 15 } );

 

 

 

三、一次插入多条数据insertMany :

 

db.restaurants.insertMany( [
   { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"},
   { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"},
   { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"},
   { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"},
   { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"},
] );

 

 判断集合是否为固定集合:db.collection.isCapped()

 

mivpn:PRIMARY> db.system.profile.isCapped()
true
mivpn:PRIMARY> db.igoodful.isCapped()
false
mivpn:PRIMARY> 

 

 

  • Write Concern

    默认情况下,Primary完成写操作即返回,Driver可通过配置Write Concern来设置写成功的规则,详情请参见Write Concern

    如下的write concern规则设置写必须在大多数节点上成功,超时时间为5秒。

     
        db.products.insert(
          { item: "envelopes", qty : 100, type: "Clasp" },
          { writeConcern: { w: majority, wtimeout: 5000 } }
        )

    上面的设置方式是针对单个请求的,也可以修改副本集默认的write concern,这样就不用单独设置每个请求。

     
        cfg = rs.conf()
        cfg.settings = {}
        cfg.settings.getLastErrorDefaults = { w: "majority", wtimeout: 5000 }
        rs.reconfig(cfg)

 

 

 

 

 

 

###################################

posted @ 2021-01-22 12:04  igoodful  阅读(189)  评论(0编辑  收藏  举报