mongodb CRUD操作

# ./mongod

2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten] MongoDB starting : pid=36578 port=27017 dbpath=/data/db 64-bit host=IBG-opensource-DB-cwT13
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten] db version v3.6.2
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten] git version: 489d177dbd0f0420a8ca04d39fd78d0a2c539420
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten] modules: none
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten] build environment:
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten]     distmod: rhel70
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten]     distarch: x86_64
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2018-04-15T18:52:36.352+0800 I CONTROL  [initandlisten] options: {}
2018-04-15T18:52:36.354+0800 I -        [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2018-04-15T18:52:36.354+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7423M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2018-04-15T18:52:36.512+0800 I STORAGE  [initandlisten] WiredTiger message [1523789556:512963][36578:0x7f6aac8c2b00], txn-recover: Main recovery loop: starting at 2/35072
2018-04-15T18:52:36.615+0800 I STORAGE  [initandlisten] WiredTiger message [1523789556:615462][36578:0x7f6aac8c2b00], txn-recover: Recovering log 2 through 3
2018-04-15T18:52:36.676+0800 I STORAGE  [initandlisten] WiredTiger message [1523789556:676715][36578:0x7f6aac8c2b00], txn-recover: Recovering log 3 through 3
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] 
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] 
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-04-15T18:52:36.740+0800 I CONTROL  [initandlisten] 
2018-04-15T18:52:36.741+0800 I CONTROL  [initandlisten] 
2018-04-15T18:52:36.741+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-15T18:52:36.741+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-15T18:52:36.741+0800 I CONTROL  [initandlisten] 
2018-04-15T18:52:36.741+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 65535 processes, 655350 files. Number of processes should be at least 327675 : 0.5 times number of files.
2018-04-15T18:52:36.741+0800 I CONTROL  [initandlisten] 
2018-04-15T18:52:36.755+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2018-04-15T18:52:36.756+0800 I NETWORK  [initandlisten] waiting for connections on port 27017

# ./mongo

MongoDB shell version v3.6.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.2
Server has startup warnings: 
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] 
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] 
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-04-15T11:01:19.795+0800 I CONTROL  [initandlisten] 
2018-04-15T11:01:19.796+0800 I CONTROL  [initandlisten] 
2018-04-15T11:01:19.796+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-15T11:01:19.796+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-15T11:01:19.796+0800 I CONTROL  [initandlisten] 
2018-04-15T11:01:19.796+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 65535 processes, 655350 files. Number of processes should be at least 327675 : 0.5 times number of files.
2018-04-15T11:01:19.796+0800 I CONTROL  [initandlisten]

https://docs.mongodb.com/manual/reference/method/db.collection.insert/

一、插入文档

插入行为,,如果集合不存在,则创建

Collection Creation:

If the collection does not currently exist, insert operations will create the collection.

方法:

  • insertOne
db.collection.insertOne(
   <document>,
   {
      writeConcern: <document>   //Optional. A document expressing the write concern. Omit to use the default write concern.
   }
)

注意:

集合不存在该操作会创建集合。

该操作不支持 db.collection.explain() 查询计划 可以使用insert 代替。

_id Field:

If the document does not specify an _id field, MongoDB adds the _id field with an ObjectId value to the new document.

ObjectId值由12个字节组成,其中前四个字节是反映ObjectId创建的时间戳,具体为:

a 4-byte value representing the seconds since the Unix epoch,

a 3-byte machine identifier,

a 2-byte process id, and

a 3-byte counter, starting with a random value.

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

{
"acknowledged" : true,         返回值
"insertedId" : ObjectId("5ad2c1572c23d0642cd35b48")
}
> db.products.find();
{ "_id" : ObjectId("5ad2c1572c23d0642cd35b48"), "item" : "card", "qty" : 15 }

> db.products.insertOne(

{ "item": "envelopes", "qty": 100, type: "Self-Sealing" },

{ writeConcern: { w : "majority", wtimeout : 100 } }    //w:majority,表示>1/2的节点有数据

)

{
"acknowledged" : true,
"insertedId" : ObjectId("5ad2cecd2c23d0642cd35b4c")
}
  • insertMany
db.collection.insertMany(
   { [ <document 1> , <document 2>, ... ] },//An array of documents to insert into the collection. 注意是数组
   {
      writeConcern: <document>,
      ordered: <boolean>   //Optional. A boolean specifying whether the mongod instance should perform an ordered or unordered insert. Defaults to true.   默认式按照顺序添加的    顺序添加的速度要慢于不按顺序添加的
   }
)

注意:

每一组操作最大可以有1000个文档。

顺序添加的速度要慢于不按顺序添加的。

不存在的集合会自动创建。

insertMany() 也不支持db.collection.explain() 可以使用insert 代替。

如果添加出错会报出 BulkWriteError exception 异常,按照顺序添加的 操作遇到错误会直接停止,而不按照顺序的会继续执行在队列中的写操作。

> db.products.insertMany( [

{ item: "card", qty: 15 },

{ item: "envelope", qty: 20 },

{ item: "stamps" , qty: 30 }

] ,  { w: "majority", wtimeout: 100 }

);

{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5ad2c2332c23d0642cd35b49"),
ObjectId("5ad2c2332c23d0642cd35b4a"),
ObjectId("5ad2c2332c23d0642cd35b4b")
]
}
  • insert
db.collection.insert(  #同时又insertOne 和insertMany的功能
   <document or array of documents>,  //数组或者文档都可以添加
   {
     writeConcern: <document>,
     ordered: <boolean> //跟insertMany()操作作用一致
   }
)

db.products.insert( [

{ item: "card", qty: 15 },

{ item: "envelope", qty: 20 },

{ item: "stamps" , qty: 30 }

] ,  { w: "majority", wtimeout: 100 }

);

db.products.insert( { item: "card", qty: 40 } );

Additional Methods for Inserts

when used with the upsert: true option:以下方法当没有查询条件匹配时创建新文档,默认是false。

db.collection.update()          
db.collection.updateOne()        
db.collection.updateMany()      
db.collection.findAndModify()    
db.collection.findOneAndUpdate()
db.collection.findOneAndReplace()
db.collection.save()
db.collection.bulkWrite()

原文地址:http://blog.51cto.com/395469372/2103665

时间: 2024-08-01 20:26:12

mongodb CRUD操作的相关文章

mongodb CRUD操作 -Select

二.查询文档 方法: db.collection.find() Additional Methods db.collection.findOne In aggregation pipeline, the $match pipeline stage provides access to MongoDB queries. db.inventory.insertMany([ { item: "journal", qty: 25, size: { h: 14, w: 21, uom: &quo

【翻译】MongoDB指南/CRUD操作(二)

[原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关注),写确认(写关注) 1 更新文档 1.1 更新 MongoDB提供下列方法用于更新一个集合 db.collection.updateOne() 更新使用指定过滤器匹配到的文档,即使过滤器匹配到多个文档,也只会更新一个文档. 3.2版本新增特性. db.collection.updateMany(

【翻译】MongoDB指南/CRUD操作(一)

[原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(一) 主要内容:CRUD操作简介,插入文档,查询文档. CRUD操作包括创建.读取.更新和删除文档. 创建操作 执行创建或者插入操作可向集合中添加文档.如果集合不存在,插入操作会创建此集合. MongoDB提供下列方法向集合中插入文档: db.collection.insert() db.collection.insertOne()  3.2版本新增 db.collection.inser

【翻译】MongoDB指南/CRUD操作(三)

[原文地址]https://docs.mongodb.com/manual/ CRUD操作(三) 主要内容: 原子性和事务(Atomicity and Transactions),读隔离.一致性和新近性,分布式查询(Distributed Queries),分布式写操作,模拟两阶段任务提交,在副本集中执行配额读取 1 原子性和事务(Atomicity and Transactions) 在MongoDB中,写操作在单文档级别具有原子性,即使修改一个文档中的多个嵌入式文档也是如此. 当一个写操作修

MongoDB对图片进行CRUD操作——与JAVA结合

上几篇博客简单对MongoDB进行了简单介绍和如何安装,以及在dos下是如何操作MongoDB和在安装MongoDB中,出现了什么错误,是如何解决的.当然这些都还不够,我们还要用到实际当中去.我用MyEclipse+JDK1.7做了一个简单的demo,来展示下MongoDB怎么运用到实际中去. MongoDB作为一个NoSql数据库的代表,存取多媒体数据,应该是强项吧?那么,在MongoDB中是如何对图片进行CRUD操作的. 上几篇博客中已经提到,MongoDB的文档结构是BSON格式,BSON

Ruby操作MongoDB(进阶)-CRUD操作

MongDB数据库的使用离不开CRUD操作.什么是CRUD,就是创建文档,读取文档信息,更新文档和删除文档. key-value键值对标记 在MongoDB的Ruby驱动中,Key_value键值多次出现.而且有时会出现语法上的巧合,这取决于在使用的Ruby版本中如何申明. 在文档创建步骤中,1.9及之后版本支持以下语法: document={name:"Tom",age:20}. 但是如果你使用的是2.2或者更高的版本,你可以用双引号将你的key包起来.如: document={&q

Mongodb的安装与CRUD操作

What is Mongodb ?         Mongo DB是一款开源的非关系型数据库(NoSql)其文档模型自由灵活,可以让你在开发过程中畅顺无比.对于大数据量.高并发.弱事务的互联网应用,MongoDB可以应对自如.MongoDB内置的水平扩展机制提供了从百万到十亿级别的数据量处理能力,完全可以满足Web2.0和移动互联网的数据存储需求,其开箱即用的特性也大大降低了中小型网站的运维成本. 安装Mongodb 来到mongodb官网http://www.mongodb.org/可以在这

MongoDB的CRUD操作

1. 前言 在上一篇文章中,我们介绍了MongoDB.现在,我们来看下如何在MongoDB中进行常规的CRUD操作.毕竟,作为一个存储系统,它的基本功能就是对数据进行增删改查操作. MongoDB中的增删改查操作,不同于我们熟悉的关系数据库中的操作.在关系数据库中,比如MySQL,我们通常使用SQL语句对数据库进行增(INSERT)删(DELETE)改(UPDATE)查(SELECT).MongoDB在对数据进行操作过程中,使用的是Document进行数据操作.在对数据库进行操作的时候,使用Do

MongoDB的一些简单CRUD操作

上篇博客已经对MongoDB进行了简单介绍和它如何安装,现在来说说MongoDB的操作.MongoDB的操作用的是Shell命令,Shell命令语法和JavaScript很类似,其实控制台底层的查询语句都是用JavaScript脚本完成操作的.使用Shell 命令,需要启动mongo.exe. 常用Shell命令如下: 1.创建collection 2.查看创建的collection 3.collection中写入数据 4.查询插入的数据 5.条件查询 6.查询一条数据中的指定列 7.排序 7.