MongoDB-crud-guide
mongodb海量数据CRUD优化

mongodb海量数据CRUD优化1. 批量保存优化避免⼀条⼀条查询,采⽤bulkWrite, 基于ReplaceOneModel,启⽤upsert:public void batchSave(List<?> spoTriples, KgInstance kgInstance) {MongoConverter converter = mongoTemplate.getConverter();List<ReplaceOneModel<Document>> bulkOperationList = spoTriples.stream().map(thing -> {org.bson.Document dbDoc = new org.bson.Document();converter.write(thing, dbDoc);ReplaceOneModel<org.bson.Document> replaceOneModel = new ReplaceOneModel(Filters.eq(UNDERSCORE_ID, dbDoc.get(UNDERSCORE_ID)),dbDoc,new UpdateOptions().upsert(true));return replaceOneModel;}).collect(Collectors.toList());mongoTemplate.getCollection(getCollection(kgInstance)).bulkWrite(bulkOperationList);}2. 分页优化经常⽤于查询的字段,需要确保建⽴了索引。
对于包含多个键的查询,可以创建符合索引。
2.1 避免不必要的count查询时,⾛索引,速度并不慢,但是如果返回分页Page<?>,需要查询totalcount,当单表数据过⼤时,count会⽐较耗时,但是设想意向,你真的需要准确的数字吗?在google、百度等搜索引擎搜索关键词时,只会给你有限的⼏个结果,因此,我们也不必给出准确的数字,设定⼀个阈值,⽐如1万,当我们发现总量⼤于1万时,返回1万,前端显⽰⼤于1万条即可。
mongo基本语法

mongo基本语法MongoDB是一种非关系型数据库管理系统,它使用文档来存储数据,而不是传统的行和列。
在MongoDB中,数据以JSON格式存储,这使得它非常灵活和适合存储各种类型的数据。
本文将介绍MongoDB的基本语法和一些常用操作。
首先,我们需要了解如何连接到MongoDB数据库。
在命令行中输入mongo命令即可连接到默认的MongoDB实例。
如果需要连接到特定的数据库,可以使用mongo 命令后加上数据库名称,例如mongo mydatabase。
一旦连接到数据库,我们就可以开始对数据进行操作。
MongoDB使用一种称为CRUD(Create, Read, Update, Delete)的操作方式来管理数据。
下面是一些基本的语法示例:1. 创建文档(Insert)要向集合中插入数据,可以使用insert方法。
例如,要向名为users的集合中插入一个文档,可以使用以下命令:ers.insert({name: "Alice", age: 25})2. 查询文档(Read)要查询文档,可以使用find方法。
例如,要查找所有年龄大于20的用户,可以使用以下命令:ers.find({age: {$gt: 20}})3. 更新文档(Update)要更新文档,可以使用update方法。
例如,要将名为Alice的用户的年龄更新为30岁,可以使用以下命令:ers.update({name: "Alice"}, {$set: {age: 30}})4. 删除文档(Delete)要删除文档,可以使用remove方法。
例如,要删除名为Alice的用户,可以使用以下命令:ers.remove({name: "Alice"})除了基本的CRUD操作之外,MongoDB还支持聚合操作、索引、事务等高级功能。
例如,可以使用aggregate方法进行数据聚合操作,使用createIndex方法创建索引,使用startSession方法开启事务等。
MongoDB CRUD Operations 与索引说明书

CRUD Operations in MongoDBCiprian Octavian Truică, Alexandru Boicea, Ionut TrifanUniversity “Politehnica” of Bucharest, Romania**********************.ro,***********************.ro,***********************Abstract- In this paper we will examine the key features of the database management system MongoDB. We will focus on the basic operations of CRUD and indexes. For our example we will create two databases one using MySQL and one in MongoDB. We will also compare the way that data will be created, selected, inserted and deleted in both databases. For the index part we will talk about the different types used in MongoDB comparing them with the indexes used in a relational database.Index Terms - MongoDB, NoSQL, BSON, CRUD, index.I. IntroductionMongoDB is a high performance and very scalable document-oriented database developed in C++ that stores data in a BSON format, a dynamic schema document structured like JSON. Mongo hits a sweet spot between the powerful query ability of a relational database and the distributed nature of other databases like Riak or HBase [1]. MongoDB was developed keeping in mind the use of the database in a distributed architecture, more specifically a Shared Nothing Architecture, and that is way it can horizontally scale and it supports Master-Slave replication and Sharding.MongoDB uses BSON to store its documents.BSON keep documents in an ordered list of elements, every elements has three components: a field name, a data type and a value. BSON was designed to be efficient in storage space and scan speed which is done for large elements in a BSON document by a prefixed with a length field. All documents must be serialized to BSON before being sent to MongoDB; they’re later deserialized from BSON by the driver into the lang uage’s native document representation [2].II.CRUD operations In MongoDBIn this chapter we will analyze the CRUD (Create, Read, Update, and Delete) operations in MongoDB. For a simple illustration of these operations we will also create a MySQL database and use the equivalent SQL queries.The database will model an article with tags and comments. The article must have author and can have many comments and tags. In other words, the relational design of the database will have the following tables:ers – the table where we will keep information aboutthe article's author.2.Articles- the table where we will store the article data3.Tags – table for tags.ments – table for comments.5.Link_article_tags – an article can have more than onetag.For the MongoDB database, all data will be stored in one collection that will have documents that will look like:{ user_id: "1",first_name: "Ciprian",last_name: "Truica",article:{date_created: "2013-05-01",text: "This is my first article",tags: ["MongoDB", "CRUD"],comments: [{author_name: "Alexandru",date_created: "2013-05-02",text: "good article"},{author_name: "Andrei",date_created: "2013-05-03",text: "interesting article"}]}});We will use the MongoDB nested property for nesting inside a document other documents. In other words document will keep tags in an object array and the comments in a BSON document array.A. Create operationIn MongoDB the create operation is used to create a collection and add new documents to a collection. In SQL the equivalent operations are CREATE and INSERT.A collection is implicitly created at the first insert: db.articles.insert(<document>). To explicitly create a new collection we can use: db.createCollection("articles").So, for creating our collection and inserting our first article we can simple use the command:db.articles.insert ({user_id: "1",first_name: "Ciprian",last_name: "Truica",article :{date_created: "2013-05-01",title: "Overview of MongoDB",text: "This is my first article",tags: ["MongoDB", "CRUD"],comments: [{author_name: "Alexandru",date_created: "2013-05-02",text: "good article"},{author_name: "Andrei",date_created: "2013-05-03",text: "interesting article"}]}});To do the same thing using a relational database we first need to create a schema, then to create the tables and lastly to do the insert operations for each table. We will only create the schema and the users table and then insert some date in this table.International Conference on Advanced Computer Science and Electronics Information (ICACSEI 2013)CREATE SCHEMA `BlogDB`;CREATE TABLE IF NOT EXISTS `BlogDB`.`users`( `id` INT NOT NULL,`first_name` VARCHAR(64) NULL ,`last_name` VARCHAR(45) NULL ,PRIMARY KEY (`id`) );INSERT INTO `BlogDB`.`users`(id, first_name, last_name) VALUES(1, "Ciprian" , "Truica" );For insertion of a new document we can write a simple JavaScript function. If we use PL/SQL or Transact-SQL this is a very difficult thing to do because we do not have a way to send arrays as input to a stored procedure or a function. A simple insertion function in MongoDB can be insertArticle, where articleText parameter is an object array and articleComments is a BSON document array:Function insertArticle (userId, firstName, lastName,articleCreateionDate, articleText, articleTags,articleComments){db.articles.insert ({ user_id: userId,first_name: firstName, last_name: lastName,article:{date_created: ISODate(articleCreateionDate),text: articleText,tags: tags,comments: articleComments}})};We can create a collection using the explicit method db.createCollection(<collection name>);. So if we want to explicitly create the collection articles we will use the next command in the mongo shell prompt:> db.createCollection ("articles");This command is equivalent to the CREATE command in SQL.B. Read OperationsRead operations are used to retrieve data from the database. They can use aggregation functions like count, distinct or aggregate.MongoDB offers two functions for this find() and findOne(). Their syntax is similar:>db.collection.find( <query>, <projection>)>db.collection.findOne(<query>, <projection>)The db.collection specifies the collection, for our example we can use db.articles. To see all the collections contain by a database we can use the show collections command in the shell prompt. The <query> argument describes the returned set and the <projection> argument describes the returned field we want. For example if we want only the comments for all the articles for user with user_id=1 we can use the next query: db.articles.find( {user_id:"1"}, { "ments" : 1});To do the same query on a relational database we need two JOIN operations:SELECT c.author_name, mentFROM `BlogDB`.`articles` AS aINNER JOIN `BlogDB`.`users` AS uON u.id = a.id_userINNER JOIN `BlogDB`.`comments` AS cON c.id_article = a.idWHERE u.id = 1;As already mentioned before, JOIN operations are very costly. In MongoDB we stored comments in an array of BSON documents, and keeping in mind that the relation between articles and comments is one to many, we actually do one projection operation using the query:>db.articles.find( {user_id:"1"}, { "ments" : 1});If we wanted only one record for the user we could have used the findOne() function.C. Update operationIn MongoDB we can use the update function for modifying a record. This function modifies a document if the record exists; otherwise an insert operation is done. For example we will change the title of the article:>db.articles.update ({_id: "1"},{$set: { "article.title": "MongoDB" }},{upsert: true});To do this simple query in SQL we will use the next statement:UPDATE `BlogDB`.`articles` SET title="MongoDB" WHERE id = 1;Let's do another example, this time we will add a new comment to the existing article:>db.articles.update ({_id: "1"},{ $addToSet:{"ments":{author_name: "Aurel", date_created: "2013-02-02",text: "MongoDB is so simple"}}},{upsert: true});MongoDB supports update operation on nested documents using $addToSet operator. The upset parameter is a Boolean. When true the update() operation will update an existing document that matches the query selection criteria or if no document matches the criteria a new document will be inserted. Another parameter that can be used with the update statement is the Boolean multi that, if the query criteria matches multiple documents and its value is set to true, then all the documents will be updated. If it value is set to false it will update only one document.An update can be done by using the save() method which is analog with an update that has upsert: true.D. Remove operationTo delete a document from a collection, in MongoDB we will use the remove() function that is analog with the DELETE operation in SQL:>db.collection.remove(<query>, <limit>);The arguments are:1. <query> is the corresponding WHERE statement in SQL2. <limit> is an Boolean argument that has the same effect asLIMIT 1 in SQLLet do a delete on our database where the record's_id is 1. The statement looks like this:>db.articles.remove ({_id: "1"});By using this statement we will not only delete the article, but also the comment and the tags for the article.In SQL this statement is a composed statement. First we will need to delete the information stored in the link_article_tags:DELETE `BlogDB`.`link_article_tags` WHERE id_article=1;Then we need to delete the comments:DELETE `BlogDB`.`comments`WHERE id_article=1;Only after these operations we can delete the article, because all the constraints have been deleted.DELETE `BlogDB`.`article`WHERE id = 1;To delete all the data from o collection we use remove() function without any where clause as is done in SQL.If we want to delete a collection from the database we will use the mongo shell prompt command db.collection.drop(). For example if we want to drop the articles collections we will use the fallowing command:>db.articles.drop();This command is equivalent to the SQL DROP command. So the corresponding query for db.articles.drop(); that will delete the articles table in SQL is:DROP TABLE `BlogDB`.`articles`;III. Indexes in MongoDBIndexes offer enhanced performance for read operations. These are useful when documents are larger than the amount of RAM available.MongoDB indexes are explicitly defined using an ensureIndex call, and any existing indices are automatically used for query processing [2].The most important index is _id, if it's not introduces at the creation of a document it is automatically created. This unique index main purpose is to be the primary key.The remaining indexes in MongoDB are known as secondary indexes. Secondary indexes can be created using ensureIndex (). If a field is a vector we can create separate indexes for each value in the array, the index is known as the multi-key index.Let's add an index to the title field for our articles:>db.articles.ensureIndex ({"article.title" : 1}, {unique : true});In SQL we would write:CREATE UNIQUE INDEX index_name ON `BlogDB`.`articles` (title);Let's create a compound index for first_name and last_name for ensuring that the select operations are done faster:>db.articles.ensureIndex ({first_name : 1, last_name:1}, {unique: true});CREATE UNIQUE INDEX compound_index ON `BlogDB`.`users` (first_name, last_name);And a last example is for a multi-key index, we will add an unique index for each separate tag:>db.articles.ensureIndex({"article.tags" : 1},{unique : true});Another type of index is the sparse index. This type of index is useful for documents that have a particular field, but that field is not in all documents in the collection, hence name. An example could be the comments field, we are not sure that all of our articles have comments but we want to add an index to this particular field. To do this we will use a sparse index: >db.articles.ensureIndex({"ments" : 1},{sparse : true});MongoDB provides geospatial indexes that are constrained by the location in a two-dimensional system and are useful for search documents that are similar to a given pair of coordinates. To declare a geospatial index a document must have stored inside a field with a two dimensional array or an embedded document, for example: loc: [x, y] or loc: {x: 1, y: 2}. To add a geospatial index one can use the fallowing command: db.collection.ensureIndex({ loc : "2d" }).Let’s add a geospatial index to our ar ticle. First we add the new information to the document and then we add the index on the new field location:>db.articles.update ({_id: "1"},{ $addToSet: {location: {x: 1, y:2}}},{upsert: true});>db.ensureIndex ({location: "2d"});To find a document based on a distance to a give point we can use a proximity query. For example, to find all the indexes near location x: 0, y: 0, one can use the fallowing query:>db.articles.find({location: {$near: [0, 0]}});To select documents that have coordinates that are in a specific geometric area one can use a bounded query. MongoDB supports the fallowing shapes: circles, rectangles and polygons. These queries perform faster than proximity queries.For each shape, we present the next examples:1. Circle with the center in (0,0) and radius=1: >db.articles.find ({ "location": { "$within": { "$center": [ [0, 0],2 ]}}})2. Rectangle bound by the point (0,0) and (2,2)> >db.articles.find ({ "location": { "$within": { "$box": [ [0, 0] , [2,2] ]}}})3. Polygon defined by three points (0,0), (22), (2,0):>db.articles.find({"location": {"$within": {"$polygon": [ [ 0,0], [2,2], [2,0] ]}}})IV.MySQL vs. MongoDB methodsTo present the MongoDB methods we have installed and configured the latest database from the developers’site. The installation and configuration was done with easily in UbuntuX6412.4LST, the operation system we have chosen to use for both databases.All SQL queries were done on the relational database management system MySQL 5.5. For a comprehensive overview we present the CRUD operations and the indexes' creation summarized in table 1.TABLE 1 CRUD OperationsV.ConclusionsMongoDB is a very flexible, schema-less database that that can be implemented in a distributed architecture. “MongoDB was build for the cloud” dev elopers boast. MongoDB can scale horizontally using Sharding. Data in a collection can split across multiple shards. Also MongoDB provides build in load balancing; data is duplicated to keep the system up and running in case of a failure. From the point of CRUD operations this fact is not seen, data will be manipulated the same and to interrogate a distributed MongoDB system will not need any other query methods. Indexes full potential is seen in a distributed system.Their main role is to help read queries perform fast. Although adding secondary indexes build more overhead in storing documents their B-tree structure is very helpful of keeping track of data that is split and stored on different servers.MongoDB supports master-slave replication. From the point of view of the CRUD operations they are not influenced in any way by the number of slaves servers a master server has. In MongoDB there is no use of a JOIN operation. Documents can be nested inside other documents. Using the no normalization encourages data redundancy, an idea not shared by most developers due to the fact that this can create confusion in a database regarding the way records are store. But using a schema-less design comes in handy when using CRUD operations; they are more natural to write and they are easier to understand at a first glance.MongoDB is a more rapid database management system. If you want a simple database that will respond very fast, this is the choice you should make[3]. To achieve scalability and mush higher performance, MongoDB gave up ACID(Atomicity, Consistency, Isolation, Durability) transaction, having a weaker concurrency model implemented known as BASE (Basically Available, Soft state, Eventual consistency). This means that updates are eventually propagated to all nodes in due time.In conclusion, if a developer wants to build a web application that is fast and flexible, than MongoDB is the right choice. If the application designer’s main concern is the relation between data and to have a normalized database that uses ACID transactions, then the right choice is a classic relational database.VI . Acknowledgments.The research presented in this paper was partially performed under the European FP7 project ERRIC (http://www.erric.eu).References[1] E. Redmond and J. R. Wilson, “Seven Databases in Seven Weeks: AGuide to Modern Databases and the NoSQL Movement”, 2012[2] K. Banker, “MongoDB in action”, 2011[3] A.Boicea, F. Rădulescu, and L.I. Agapin, “MongoDB vs. Oracle -database comparison”, The 3-rd Conference EIDWT, Bucharest, 2012 [4] /。
mongoDB 入门指南、示例

mongoDB 技术文档介绍:MongoDB是一个基于分布式文件存储的数据库。
由C++语言编写。
旨在为WEB应用提供可扩展的高性能数据存储解决方案。
特点:高性能、易部署、易使用,存储数据非常方便。
主要功能特性有:面向集合存储,易存储对象类型的数据模式自由支持动态查询支持完全索引,包含内部对象支持查询支持复制和故障恢复使用高效的二进制数据存储,包括大型对象(如视频等)自动处理碎片,以支持云计算层次的扩展性支持RUBY,PYTHON,JAVA,C++,PHP等多种语言文件存储格式为BSON(一种JSON的扩展)可通过网络访问使用原理所谓“面向集合”(Collenction-Oriented),意思是数据被分组存储在数据集中,被称为一个集合(Collenction)。
每个集合在数据库中都有一个唯一的标识名,并且可以包含无限数目的文档。
集合的概念类似关系型数据库(RDBMS)里的表(table),不同的是它不需要定义任何模式(schema)。
模式自由(schema-free),意味着对于存储在mongodb数据库中的文件,我们不需要知道它的任何结构定义。
如果需要的话,你完全可以把不同结构的文件存储在同一个数据库里。
存储在集合中的文档,被存储为键-值对的形式。
键用于唯一标识一个文档,为字符串类型,而值则可以是各种复杂的文件类型。
我们称这种存储形式为BSON(Binary Serialized dOcument Format)。
一、准备工作1、下载mongoDB下载地址:/downloads选择合适你的版本相关文档:/display/DOCS/Tutorial2、安装mongoDBA、不解压模式:将下载下来的mongoDB-xxx.zip打开,找到bin目录,运行mongod.exe就可以启动服务,默认端口27017,db保存的路径是系统C硬盘目录的根目录的/data/db目录。
也就是说,如果你的mongoDB-xxx.zip在E盘,那么你需要在C盘下建立data/db目录。
mongodb——文档操作

mongodb——⽂档操作⽂档基本CRUD⽂档(document)的数据结构是和JSON类似的BSON⽂档的插⼊单个⽂档的插⼊使⽤insert()或save()⽅法向集合插⼊⽂档,语法如下:db.collection.insert(<document or array of ducuments>,{writeConcern: <document>,ordered: <boolean>})Parameter Type Descriptiondocument documentor array要插⼊到集合中的⽂档或⽂档数组writeConcern documentordered bolean如果为真,则按顺序插⼊数组中的⽂档,如果其中⼀个⽂档出现错误,MongoDB将返回⽽不处理数组中的其余⽂档。
如果为假,则执⾏⽆序插⼊,如果其中⼀个⽂档出现错误,则继续处理数组中的主⽂档。
2.6+默认为true【⽰例】向comment的集合中插⼊⼀条测试数据> ment.insert({"articleId": 10000, "content": "今天天⽓真好,阳光明媚", "userId": 1001, "nickname": "Rose", "createdatetime": new Date(), "lukenum": NumberInt(10), "state": null}) WriteResult({ "nInserted" : 1 })批量插⼊语法:db.collection.insertMany([<document 1>, <document 2>, ...]{writeConcern: <document>,ordered: <boolean>})⽂档的基本查询查询数据的语法格式如下:db.collection.find(<query>, [projection])参数:Parameter Type Descriptionquery documentprojection document【实例】查询所有> ment.find()> ment.find({})这⾥你会发现每条⽂档都会有⼀个_id字段,这个相当于我们原来关系型数据表的主键。
mongodb curd 手册

很高兴接到您的文章写手任务,让我们开始撰写关于 MongoDB CURD 手册的高质量文章。
1. MongoDB CURD 是指什么?MongoDB CURD 是对 MongoDB 数据库常用操作的缩写,其中包括Create(创建)、Update(更新)、Read(读取)和Delete(删除)四个操作。
这些操作是在 MongoDB 数据库中对数据进行管理和操作时常用的基本操作。
2. Create(创建)在 MongoDB 中,创建数据是非常常见的操作。
您可以通过指定集合和字段来创建新的文档。
在进行创建操作时,可以指定字段的值,并且还可以在创建文档时指定文档的唯一标识符。
3. Update(更新)更新操作是在 MongoDB 中对已存在的文档进行更新。
您可以通过指定条件来定位需要更新的文档,并且可以根据需要更新文档中的字段值。
更新操作也可以用来更新多个文档,以及根据不同的条件来进行更新操作。
4. Read(读取)读取操作是指从 MongoDB 数据库中检索数据的操作。
您可以通过指定条件来获取符合条件的文档,并且可以根据需要对结果进行排序、分页、投影等操作。
读取操作还可以进行聚合操作,以及对数据进行统计分析。
5. Delete(删除)删除操作是在 MongoDB 中对不再需要的文档进行删除。
您可以通过指定条件来定位需要删除的文档,并且可以根据需要删除单个或多个文档。
删除操作还可以对集合进行删除,以及删除整个数据库。
总结回顾:通过本文我们对 MongoDB CURD 进行了深入的探讨,包括了Create、Update、Read 和 Delete 四个操作,并且针对每个操作进行了详细的介绍。
在实际使用 MongoDB 数据库时,这些 CURD 操作是非常重要的,能帮助我们对数据进行管理和操作。
MongoDB CURD 手册是每个开发者在使用 MongoDB 时不可或缺的工具。
个人观点和理解:作为一名资深的 MongoDB 开发者,我对 MongoDB CURD 操作有着丰富的实践经验。
【MongoDB配置篇】MongoDB配置文件详解

【MongoDB配置篇】MongoDB配置⽂件详解⽬录MongoDB实例的运⾏离不开相应的参数配置,⽐如对数据库存放路径dbpath的配置,对于参数的配置,可以在命令⾏以选项的形式进⾏配置,也可以将配置信息列⼊配置⽂件进⾏配置。
但是,使⽤配置⽂件将会使对mongod和mongos的管理变得更加容易,本篇将会对配置⽂件进⾏详细的讲解。
1 数据库环境[mongod@strong ~]$ mongod --versiondb version v4.2.0git version: a4b751dcf51dd249c5865812b390cfd1c0129c30OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013allocator: tcmallocmodules: nonebuild environment:distmod: rhel62distarch: x86_64target_arch: x86_642 配置⽂件2.1 配置⽂件格式MongoDB配置⽂件使⽤YAML的格式。
2.2 配置⽂件的使⽤对于配置⽂件的使⽤,在mongod或mongos中指定--config或-f选项。
1)指定--config选项[mongod@strong ~]$ mongod --config /etc/f2)指定-f选项[mongod@strong ~]$ mongod -f /etc/f3 配置⽂件核⼼选项3.1 systemLog选项1)选项systemLog:verbosity: <int>quiet: <boolean>traceAllExceptions: <boolean>syslogFacility: <string>path: <string>logAppend: <boolean>logRotate: <string>destination: <string>timeStampFormat: <string>component:accessControl:verbosity: <int>command:verbosity: <int># COMMENT additional component verbosity settings omitted for brevity2)说明verbosity:默认为0,值范围为0-5,⽤于输出⽇志信息的级别,值越⼤,输出的信息越多;quiet:mongod或mongos运⾏的模式,在该模式下限制输出的信息,不推荐使⽤该模式;traceAllExceptions:打印详细信息以便进⾏调试;path:⽇志⽂件的路径,mongod或mongos会将所有诊断⽇志信息发送到该位置,⽽不是标准输出或主机的syslog上;logAppend:默认为false,若设为true,当mongod或mongos实例启动时,会将新的条⽬追加到已存在的⽇志⽂件,否则,mongod会备份已存在的⽇志,并创建新的⽇志⽂件;destination:指定⽇志输出的⽬的地,具体值为file或syslog,若设置为file,需指定path,该选项未指定,则将所有⽇志输出到标准输出;timeStampFormat:⽇志信息中的时间格式,默认为iso8601-local,该选项有三个值,分别为ctime、iso8601-utc和iso8601-local;3.2 processManagement选项1)选项processManagement:fork: <boolean>pidFilePath: <string>timeZoneInfo: <string>2)说明fork:默认值为false,设置为true,会激活守护进程在后台运⾏mongod或mongos进程;pidFilePath:指定mongod或mongos写PID⽂件的路径,不指定该值,则不会创建PID⽂件;3.3 cloud选项1)选项cloud:monitoring:free:state: <string>tags: <string>2)说明state:激活或禁⽤免费的MongoDB Cloud监控,该选项有以下三个值,分别为runtime、on和off,默认为runtime;在运⾏时可以通过db.enableFreeMonitoring()和db.disableFreeMonitoring()tags:描述环境上下⽂的可选标记;3.4 net选项1)选项net:port: <int>bindIp: <string>bindIpAll: <boolean>maxIncomingConnections: <int>wireObjectCheck: <boolean>ipv6: <boolean>unixDomainSocket:enabled: <boolean>pathPrefix: <string>filePermissions: <int>tls:certificateSelector: <string>clusterCertificateSelector: <string>mode: <string>certificateKeyFile: <string>certificateKeyFilePassword: <string>clusterFile: <string>clusterPassword: <string>CAFile: <string>clusterCAFile: <string>CRLFile: <string>allowConnectionsWithoutCertificates: <boolean>allowInvalidCertificates: <boolean>allowInvalidHostnames: <boolean>disabledProtocols: <string>FIPSMode: <boolean>compression:compressors: <string>serviceExecutor: <string>2)说明port:MongoDB实例监听客户端连接的TCP端⼝,对于mongod或mongos实例,默认端⼝为27017,对于分⽚成员,默认端⼝为27018,对于配置服务器成员,默认端⼝为27019;bindIp:默认值为localhost。
MongoDB_使用手册-中文版

MongoDB_使用手册-中文版MongoDB 使用手册-中文版1:简介1.1 MongoDB 简介1.2 MongoDB 的优势1.3 安装 MongoDB1.4 启动和关闭 MongoDB2:数据库操作2.1 创建数据库2.2 切换数据库2.3 删除数据库2.4 数据库的备份和还原2.5 数据库的访问控制3:集合操作3.1 创建集合3.2 删除集合3.3 查找集合3.4 更新集合3.5 排序和限制集合结果4:文档操作4.1 插入文档4.2 查询文档4.3 更新文档4.4 删除文档4.5 索引和性能优化5:聚合操作5.1 聚合管道5.2 查询优化技巧5.3 数据分析和处理6:数据备份和恢复6.1 数据备份策略6.2 数据恢复方法7:复制和分片7.1 复制集7.2 分片集群8:安全性和权限控制8.1 认证和授权8.2 数据加密8.3 安全配置建议9: MongoDB 驱动程序9.1 Python 驱动程序 9.2 Java 驱动程序9.3 Node:js 驱动程序 9.4 :NET 驱动程序10:性能调优10:1 集合级别的优化 10:2 查询优化10:3 索引优化10:4 内存和磁盘配置11:故障排除11.1 常见问题11.2 日志分析11.3 性能监控12: MongoDB 与关系型数据库的比较12.1 数据模型比较12.2 查询语言比较12.3 事务和一致性比较本文档涉及附件:1:示例代码文件:[附件1](附件1:zip)2:配置文件示例:[附件2](附件2:txt)本文所涉及的法律名词及注释:1:认证和授权:指通过身份验证和权限控制来确保只有经过授权的用户才能访问和操作数据库的过程。
2:数据加密:指使用加密算法对数据库中的敏感数据进行加密保护的过程。
3:复制集:指一组 MongoDB 服务器的集合,其中包含主服务器(primary)和多个副本服务器(secondary),用于提供数据冗余和高可用性支持。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
MongoDB CRUD OperationsRelease2.4.6MongoDB Documentation ProjectSeptember13,2013 Contents1MongoDB CRUD Introduction31.1Database Operations (3)Query (3)Data Modification (5)1.2Related Features (5)/indexes (5)/core/read-preference (6)Write Concern (6)/aggregation (6)2MongoDB CRUD Concepts62.1Read Operations (6)Query Interface (7)Query Behavior (7)Query Statements (8)Projections (8)Related Concepts (10)2.2Write Operations (17)Create (18)Update (19)Delete (20)Isolation of Write Operations (21)Related Concepts (21)3MongoDB CRUD Tutorials323.1Insert Documents (32)Insert a Document with insert()Method (32)Insert a Document with update()Method (33)Insert a Document with save()Method (33)3.2Query Documents (33)Select All Documents in a Collection (33)Specify Equality Condition (34)Specify Conditions Using Query Operators (34)Specify AND Conditions (34)Specify OR Conditions (34)Specify AND as well as OR Conditions (35)Subdocuments (35)Arrays (36)3.3Limit Fields to Return from a Query (37)Return All Fields in Matching Documents (37)Return the Specified Fields and the_id Field Only (38)Return Specified Fields Only (38)Return All But the Excluded Field (38)Projection for Array Fields (38)3.4Iterate a Cursor in the mongo Shell (38)Manually Iterate the Cursor (38)Iterator Index (39)3.5Analyze Query Performance (40)Evaluate the Performance of a Query (40)Compare Performance of Indexes (40)3.6Modify Documents (41)Modify Multiple Documents with update()Method (41)Modify a Document with save()Method (41)3.7Remove Documents (42)Remove All Documents (42)Remove Documents that Matches a Condition (42)Remove a Single Document that Matches a Condition (42)3.8Perform Two Phase Commits (42)Synopsis (42)Background (43)Pattern (43)Using Two-Phase Commits in Production Applications (47)3.9Create Tailable Cursor (48)Overview (48)C++Example (48)3.10Isolate Sequence of Operations (50)Overview (50)Update if Current (50)3.11Create an Auto-Incrementing Sequence Field (51)Synopsis (51)3.12Limit Number of Elements in an Array after an Update (55)Synopsis (55)Pattern (55)4MongoDB CRUD Reference564.1Query Cursor Methods (56)4.2Query and Data Manipulation Collection Methods (56)4.3MongoDB CRUD Reference Documentation (56)Documents (57)MongoDB Drivers and Client Libraries (60)Write Concern Reference (61)SQL to MongoDB Mapping Chart (63)ObjectId (68)BSON Types (70)MongoDB Extended JSON (73)GridFS Reference (75)The bios Example Collection (76)Index82 2MongoDB provides rich semantics for reading and manipulating data.CRUD stands for create,read,update,and delete.These terms are the foundation for all interactions with the database.MongoDB CRUD Introduction(page3)An introduction to the MongoDB data model as well as queries and data manipulations.MongoDB CRUD Concepts(page6)The core documentation of query and data manipulation.MongoDB CRUD Tutorials(page32)Examples of basic query and data modification operations.MongoDB CRUD Reference(page56)Reference material for the query and data manipulation interfaces.1MongoDB CRUD IntroductionMongoDB stores data in the form of documents,which are JSON-likefield and value pairs.Documents are analogous to structures in programming languages that associate keys with values,where keys may nest other pairs of keys and values(e.g.dictionaries,hashes,maps,and associative arrays).Formally,MongoDB documents are BSON documents, which is a binary representation of JSON with additional type information.For more information,see Documents (page57).Figure1:A MongoDB document.MongoDB stores all documents in collections.A collection is a group of related documents that have a set of shared common indexes.Collections are analogous to a table in relational databases.1.1Database OperationsQueryIn MongoDB a query targets a specific collection of documents.Queries specify criteria,or conditions,that identify the documents that MongoDB returns to the clients.A query may include a projection that specifies thefields from the matching documents to return.You can optionally modify queries to impose limits,skips,and sort orders.In the following diagram,the query process specifies a query criteria and a sort modifier:3Figure2:A collection of MongoDB documents.4Data ModificationData modification refers to operations that create,update,or delete data.In MongoDB,these operations modify the data of a single collection.For the update and delete operations,you can specify the criteria to select the documents to update or remove.In the following diagram,the insert operation adds a new document to the users collection.Figure4:The stages of a MongoDB insert operation.1.2Related Features/indexesTo enhance the performance of common queries and updates,MongoDB has full support for secondary indexes.These indexes allow applications to store a view of a portion of the collection in an efficient data structure.Most indexes store an ordered representation of all values of afield or a group offields.Indexes may also enforce uniqueness,store objects in a geospatial representation,and facilitate text search.5/core/read-preferenceFor replica sets and sharded clusters with replica set components,applications specify read preferences.A read preference determines how the client direct read operations to the set.Write ConcernApplications can also control the behavior of write operations using write concern(page21).Particularly useful for deployments with replica sets,the write concern semantics allow clients to specify the assurance that MongoDB provides when reporting on the success of a write operation./aggregationIn addition to the basic queries,MongoDB provides several data aggregation features.For example,MongoDB can return counts of the number of documents that match a query,or return the number of distinct values for afield,or process a collection of documents using a versatile stage-based data processing pipeline or map-reduce operations. 2MongoDB CRUD ConceptsThe Read Operations(page6)and Write Operations(page17)documents introduces the behavior and operations of read and write operations for MongoDB deployments.Read Operations(page6)Introduces all operations that select and return documents to clients,including the query specifications.Cursors(page10)Queries return interable objects,called cursors,that hold the full result set of the query request.Query Optimization(page12)Analyze and improve query performance.Distributed Queries(page14)Describes how sharded clusters and replica sets affect the performance of read operations.Write Operations(page17)Introduces data create and modify operations,their behavior,and performances.Write Concern(page21)Describes the kind of guarantee MongoDB provides when reporting on the success of a write operation.Distributed Write Operations(page24)Describes how MongoDB directs write operations on sharded clusters and replica sets and the performance characteristics of these operations.2.1Read OperationsRead operations,or queries,retrieve data stored in the database.In MongoDB,queries select documents from a single collection.Queries specify criteria,or conditions,that identify the documents that MongoDB returns to the clients.A query may include a projection that specifies thefields from the matching documents to return.The projection limits the amount of data that MongoDB returns to the client over the network.6Query InterfaceFor query operations,MongoDB provide a db.collection.find()method.The method accepts both the query criteria and projections and returns a cursor(page10)to the matching documents.You can optionally modify the query to impose limits,skips,and sort orders.The following diagram highlights the components of a MongoDB query operation:Figure5:The components of a MongoDBfind operation.The next diagram shows the same query in SQL:Figure6:The components of a SQL SELECT statement.ExampleThis query selects the documents in the users collection that match the condition age is greater than18.To specify the greater than condition,query criteria uses the greater than(i.e.$gt)query selection operator.The query returns at most5matching documents(or more precisely,a cursor to those documents).The matching documents will return with only the_id,name and addressfields.See Projections(page8)for details.SeeSQL to MongoDB Mapping Chart(page63)for additional examples of MongoDB queries and the corresponding SQL statements.Query BehaviorMongoDB queries exhibit the following behavior:•All queries in MongoDB address a single collection.•You can modify the query to impose limits,skips,and sort orders.7•The order of documents returned by a query is not defined and is not necessarily consistent unless you specify a sort().•Operations that modify existing documents(page41)(i.e.updates)use the same query syntax as queries to select documents to update.•In aggregation pipeline,the$match pipeline stage provides access to MongoDB queries.MongoDB provides a db.collection.findOne()method as a special case of find()that returns a single document.Query StatementsConsider the following diagram of the query process that specifies a query criteria and a sort modifier:Figure7:The stages of a MongoDB query with a query criteria and a sort modifier.In the diagram,the query selects documents from the users ing a query selection operator to define the conditions for matching documents,the query selects documents that have age greater than(i.e.$gt) 18.Then the sort()modifier sorts the results by age in ascending order.For additional examples of queries,see Query Documents(page33).ProjectionsQueries in MongoDB return allfields in all matching documents by default.To limit the amount of data that MongoDB sends to applications,include a projection in the queries.By projecting results with a subset offields,applications reduce their network overhead and processing requirements.Projections,which are the the second argument to the find()method,may either specify a list offields to return or listfields to exclude in the result documents.Important:Except for excluding the_idfield in inclusive projections,you cannot mix exclusive and inclusive projections.8Consider the following diagram of the query process that specifies a query criteria and a projection:Figure8:The stages of a MongoDB query with a query criteria and projection.MongoDB only transmits the projected data to the clients.In the diagram,the query selects from the users collection.The criteria matches the documents that have age equal to18.Then the projection specifies that only the namefield should return in the matching documents.Projection ExamplesExclude One Field From a Result SetThis query selects a number of documents in the records collection that match the query{"user_id":{ $lt:42}},but excludes the historyfield.Return Twofields and the_id FieldThis query selects a number of documents in the records collection that match the query{"user_id":{ $lt:42}},but returns documents that have the_idfield(implicitly included)as well as the name and email fields.Return Two Fields and Exclude_idThis query selects a number of documents in the records collection that match the query{"user_id":{ $lt:42}},but only returns the name and emailfields.9SeeLimit Fields to Return from a Query(page37)for more examples of queries with projection statements.Projection BehaviorMongoDB projections have the following properties:•In MongoDB,the_idfield is always included in results unless explicitly excluded.•Forfields that contain arrays,MongoDB provides the following projection operators:$elemMatch,$slice, $.•For related projection functionality in the aggregation framework pipeline,use the$project pipeline stage.Related ConceptsThe following documents further describe read operations:Cursors(page10)Queries return interable objects,called cursors,that hold the full result set of the query request. Query Optimization(page12)Analyze and improve query performance.Query Plans(page13)MongoDB processes and executes using plans developed to return results as efficiently as possible.Distributed Queries(page14)Describes how sharded clusters and replica sets affect the performance of read opera-tions.CursorsIn the mongo shell,the primary method for the read operation is the db.collection.find()method.This method queries a collection and returns a cursor to the returning documents.To access the documents,you need to iterate the cursor.However,in the mongo shell,if the returned cursor is not assigned to a variable using the var keyword,then the cursor is automatically iterated up to20times1to print up to thefirst20documents in the results.For example,in the mongo shell,the following read operation queries the inventory collection for documents that have type equal to’food’and automatically print up to thefirst20matching documents:To manually iterate the cursor to access the documents,see Iterate a Cursor in the mongo Shell(page38).Cursor Behaviors1You can use the DBQuery.shellBatchSize to change the number of iteration from the default value20.See mongo-shell-executing-queries for more information.10Closure of Inactive Cursors By default,the server will automatically close the cursor after10minutes of inactivity or if client has exhausted the cursor.To override this behavior,you can specify the noTimeout wire protocolflag2 in your query;however,you should either close the cursor manually or exhaust the cursor.In the mongo shell,you can set the noTimeoutflag:See your driver(page60)documentation for information on setting the noTimeoutflag.For the mongo shell,see cursor.addOption()for a complete list of available cursorflags.Cursor Isolation Because the cursor is not isolated during its lifetime,intervening write operations on a document may result in a cursor that returns a document more than once if that document has changed.To handle this situation, see the information on snapshot mode.Cursor Batches The MongoDB server returns the query results in batches.Batch size will not exceed the maximum BSON document size.For most queries,thefirst batch returns101documents or just enough documents to exceed1 megabyte.Subsequent batch size is4megabytes.To override the default size of the batch,see batchSize()and limit().For queries that include a sort operation without an index,the server must load all the documents in memory to perform the sort and will return all documents in thefirst batch.As you iterate through the cursor and reach the end of the returned batch,if there are more results,cursor.next() will perform a getmore operation to retrieve the next batch.To see how many documents remain in the batch as you iterate the cursor,you can use the objsLeftInBatch()method,as in the following example:Cursor Information You can use the command cursorInfo to retrieve the following information on cursors:•total number of open cursors•size of the client cursors in current use•number of timed out cursors since the last server restartConsider the following example:The result from the command returns the following document:2/meta-driver/latest/legacy/mongodb-wire-protocolQuery OptimizationIndexes improve the efficiency of read operations by reducing the amount of data that query operations need to process. This simplifies the work associated with fulfilling queries within MongoDB.Create an Index to Support Read Operations If your application queries a collection on a particularfield orfields, then an index on the queriedfield orfields can prevent the query from scanning the whole collection tofind and return the query results.For more information about indexes,see the complete documentation of indexes in MongoDB.ExampleAn application queries the inventory collection on the typefield.The value of the typefield is user-driven.To improve the performance of this query,add an ascending,or a descending,index to the inventory collection on the typefield.3In the mongo shell,you can create indexes using the db.collection.ensureIndex() method:This index can prevent the above query on type from scanning the whole collection to return the results.To analyze the performance of the query with an index,see Analyze Query Performance(page40).In addition to optimizing read operations,indexes can support sort operations and al-low for a more efficient storage utilization.See db.collection.ensureIndex()and /manualadministration/indexes for more information about index creation.Query Selectivity Some query operations are not selective.These operations cannot use indexes effectively or cannot use indexes at all.The inequality operators$nin and$ne are not very selective,as they often match a large portion of the index.As a result,in most cases,a$nin or$ne query with an index may perform no better than a$nin or$ne query that must scan all documents in a collection.Queries that specify regular expressions,with inline JavaScript regular expressions or$regex operator expressions, cannot use an index with one exception.Queries that specify regular expression with anchors at the beginning of a string can use an index.Covering a Query An index covers a query,a covered query,when:•all thefields in the query(page33)are part of that index,and•all thefields returned in the documents that match the query are in the same index.For these queries,MongoDB does not need to inspect documents outside of the index.This is often more efficient than inspecting entire documents.ExampleGiven a collection inventory with the following index on the type and itemfields:3For single-field indexes,the selection between ascending and descending order is immaterial.For compound indexes,the selection is important. See indexing order for more details.This index will cover the following query on the type and itemfields,which returns only the itemfield:However,the index will not cover the following query,which returns the itemfield and the_idfield:See indexes-covered-queries for more information on the behavior and use of covered queries.Query PlansThe MongoDB query optimizer processes queries and chooses the most efficient query plan for a query given the avail-able indexes.The query system then uses this query plan each time the query runs.The query optimizer occasionally reevaluates query plans as the content of the collection changes to ensure optimal query plans.You can use the explain()method to view statistics about the query plan for a given query.This information can help as you develop indexing strategies.Query Optimization To create a new query plan,the query optimizer:1.runs the query against several candidate indexes in parallel.2.records the matches in a common results buffer or buffers.•If the candidate plans include only ordered query plans,there is a single common results buffer.•If the candidate plans include only unordered query plans,there is a single common results buffer.•If the candidate plans include both ordered query plans and unordered query plans,there are two commonresults buffers,one for the ordered plans and the other for the unordered plans.If an index returns a result already returned by another index,the optimizer skips the duplicate match.In the case of the two buffers,both buffers are de-duped.3.stops the testing of candidate plans and selects an index when one of the following events occur:•An unordered query plan has returned all the matching results;or•An ordered query plan has returned all the matching results;or•An ordered query plan has returned a threshold number of matching results:–Version2.0:Threshold is the query batch size.The default batch size is101.–Version2.2:Threshold is101.The selected index becomes the index specified in the query plan;future iterations of this query or queries with the same query pattern will use this index.Query pattern refers to query select conditions that differ only in the values,as in the following two queries with the same query pattern:Query Plan Revision As collections change over time,the query optimizer deletes the query plan and re-evaluates after any of the following events:•The collection receives1,000write operations.•The reIndex rebuilds the index.•You add or drop an index.•The mongod process restarts.Distributed QueriesRead Operations to Sharded Clusters Sharded clusters allow you to partition a data set among a cluster of mongod instances in a way that is nearly transparent to the application.For an overview of sharded clusters,see the /manualsharding section of this manual.For a sharded cluster,applications issue operations to one of the mongos instances associated with the cluster.Figure9:Diagram of a sharded cluster.Read operations on sharded clusters are most efficient when directed to a specific shard.Queries to sharded collections should include the collection’s shard key.When a query includes a shard key,the mongos can use cluster metadatafrom the config database to route the queries to shards.Figure10:Read operations to a sharded cluster.Query criteria includes the shard key.The query router mongos cantarget the query to the appropriate shard or shards.If a query does not include the shard key,the mongos must direct the query to all shards in the cluster.These scattergather queries can be inefficient.On larger clusters,scatter gather queries are unfeasible for routine operations.For more information on read operations in sharded clusters,see the /manualcore/sharded-clu and sharding-shard-key sections.Read Operations to Replica Sets Replica sets use read preferences to determine where and how to route readoperations to members of the replica set.By default,MongoDB always reads data from a replica set’s primary.Youcan modify that behavior by changing the read preference mode.You can configure the read preference mode on a per-connection or per-operation basis to allow reads from secondariesto:•reduce latency in multi-data-center deployments,•improve read throughput by distributing high read-volumes(relative to write volume),•for backup operations,and/ormongos must broadcast query to all shards for the collection.•to allow reads during failover situations.Figure12:Read operations to a replica set.Default read preference routes the read to the primary.Read preference of nearest routes the read to the nearest member.Read operations from secondary members of replica sets are not guaranteed to reflect the current state of the primary, and the state of secondaries will trail the primary by some amount of time.Often,applications don’t rely on this kind of strict consistency,but application developers should always consider the needs of their application before setting read preference.For more information on read preference or on the read preference modes,see /manualcore/read-preference and replica-set-read-preference-modes.2.2Write OperationsA write operation is any operation that creates or modifies data in the MongoDB instance.In MongoDB,write operations target a single collection.All write operations in MongoDB are atomic on the level of a single document.There are three classes of write operations in MongoDB:insert,update,and remove.Insert operations add new data to a collection.Update operations modify an existing data,and remove operations delete data from a collection.No insert,update,or remove can affect more than one document atomically.For the update and remove operations,you can specify criteria,or conditions,that identify the documents to update or remove.These operations use the same query syntax to specify the criteria as read operations(page6).After issuing these modification operations,MongoDB allows applications to determine the level of acknowledgment returned from the database.See Write Concern(page21).CreateCreate operations add new documents to a collection.In MongoDB,the db.collection.insert()method perform create operations.The following diagram highlights the components of a MongoDB insert operation:Figure13:The components of a MongoDB insert operations.The following diagram shows the same query in SQL:Figure14:The components of a SQL INSERT statement.ExampleThis operation inserts a new documents into the users collection.The new document has fourfields name,age, and status,and an_idfield.MongoDB always adds the_idfield to the new document if thefield does not exist. For more information,see db.collection.insert()and Insert Documents(page32).An upsert is an operation that performs either an update of an existing document or an insert of a new docu-ment if the document to modify does not exist.With an upsert,applications do not need to make two sep-arate calls to the database in order to decide between performing an update or an insert operation.Both the db.collection.update()method and the db.collection.save()method can perform an upsert.Seedb.collection.update()and db.collection.save()for details on performing an upsert with these methods.SeeSQL to MongoDB Mapping Chart(page63)for additional examples of MongoDB write operations and the corre-sponding SQL statements.Insert BehaviorIf you add a new document without the_idfield,the client library or the mongod instance adds an_idfield and populates thefield with a unique ObjectId.If you specify the_idfield,the value must be unique within the collection.For operations with write concern (page21),if you try to create a document with a duplicate_id value,mongod returns a duplicate key exception. UpdateUpdate operations modify existing documents in a collection.In MongoDB,db.collection.update()and the db.collection.save()methods perform update operations.The db.collection.update()method can accept a query criteria to determine which documents to update as well as an option to update multiple rows.The method can also accept options that affect its behavior such as the multi option to update multiple documents.The following diagram highlights the components of a MongoDB update operation:Figure15:The components of a MongoDB update operation.The following diagram shows the same query in SQL:Figure16:The components of a SQL UPDATE statement.ExampleThis update operation on the users collection sets the statusfield to A for the documents that match the criteria of age greater than18.For more information,see db.collection.update()and db.collection.save(),and Modify Docu-ments(page41)for examples.Update BehaviorBy default,the db.collection.update()method updates a single document.However,with the multi option, update()can update all documents in a collection that match a query.The db.collection.update()method either updates specificfields in the existing document or replaces the document.See db.collection.update()for details.When performing update operations that increase the document size beyond the allocated space for that document,the update operation relocates the document on disk and may reorder the documentfields depending on the type of update. The db.collection.save()method replaces a document and can only update a single document.See db.collection.save()and Insert Documents(page32)for more informationDeleteDelete operations remove documents from a collection.In MongoDB,db.collection.remove()method per-forms delete operations.The db.collection.remove()method can accept a query criteria to determine which documents to remove.The following diagram highlights the components of a MongoDB remove operation:Figure17:The components of a MongoDB remove operation.The following diagram shows the same query in SQL:Figure18:The components of a SQL DELETE statement.。