MongoDB学习日记(四):CRUD - insert

之前的笔记也提到了,我这里学习使用的是3.0.6版本的,与之前的版本有一定的差异,建议去官网学习教程,学习地址:MongoDB官网CRUD学习教程(ps:全英文),如果你觉得看不懂,那么我推荐你去看这个教程:易百的教程

Insert( ) :
> db.collection.insert()

前面创建 collection 时也提到了,insert( ) 方法会自动创建不存在的 collection . 另外之前也提到了 MongoDB 的客户端也是一个 js 编译器,我们可以创建一个对象,然后插入。还有就是 MongoDB 的 collection 不会像表那样,insert( ) 插入的值没有限定 。

> var document = {
>    "name" : "admin",
>    "age"  : 20,
>    "address" : {
>       "privonce" : "shanghai",
>       "city" : "shanghai"
>    },
>    "group" : [ "news" , "sport" ]
> }
> 
> db.user.insert( document )
WriteResult({ "nInserted" : 1 })
> document.name = "system"
system
> db.user.insert( document )
WriteResult({ "nInserted" : 1 })

代码演示

图中也可以看到 MongoDB 会自动创建“_id”,它的类型是 ObjectId,较早的版本是不支持多条插入的,现在可以了

> var documents = [
> {
>    "name" : "test1",
>    "age"  : 20,
>    "address" : {
>       "privonce" : "shanghai",
>       "city" : "shanghai"
>    },
>    "group" : [ "news" , "sport" ]
> },{
>    "name" : "test2",
>    "age"  : 20,
>    "address" : {
>       "privonce" : "shanghai",
>       "city" : "shanghai"
>    },
>    "group" : [ "news" , "sport" ]
> }
> ];
> db.user.insert(document)

插入后返回一个对象的运行状态bulkwriteresult。一个成功的插入文件返回以下信息:

BulkWriteResult({
   "writeErrors" : [ ],
   "writeConcernErrors" : [ ],
   "nInserted" : 2,
   "nUpserted" : 0,
   "nMatched" : 0,
   "nModified" : 0,
   "nRemoved" : 0,
   "upserted" : [ ]
})

代码示例


Bulk

Bulk 这种模式更像 MySql 的添加操作,个人觉得知道 insert( ) 就足够了,我这里就引用官网的例子。

/* 创建 bulk 对象 */
var bulk = db.inventory.initializeUnorderedBulkOp();

/* 添加插入数据 */
bulk.insert(
   {
     item: "BE10",
     details: { model: "14Q2", manufacturer: "XYZ Company" },
     stock: [ { size: "L", qty: 5 } ],
     category: "clothing"
   }
);
bulk.insert(
   {
     item: "ZYT1",
     details: { model: "14Q1", manufacturer: "ABC Company"  },
     stock: [ { size: "S", qty: 5 }, { size: "M", qty: 5 } ],
     category: "houseware"
   }
);

/* 提交插入 */
bulk.execute();

/* 插入返回结果 */
BulkWriteResult({
   "writeErrors" : [ ],
   "writeConcernErrors" : [ ],
   "nInserted" : 2,
   "nUpserted" : 0,
   "nMatched" : 0,
   "nModified" : 0,
   "nRemoved" : 0,
   "upserted" : [ ]
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值