By default, mongo looks for a database server listening on port 27017 on the localhost interface. To connect to a server on a different port or interface, use the --port and --host options.
After starting the mongo shell your session will use the test database by default. At any time, issue the following operation at the mongo to report the name of the current database: db
At this point, if you issue the show dbs operation again, it will not include the mydb database. MongoDB will not permanently create a database until you insert data into that database.
用mongo连接上mongod实例时,默认使用test db,需要用use [db_name]命令,切换到目标数据库。除非确实插入文档,否则不会真正创建数据库,用show dbs命令暂时不会看见
When you access documents in a cursor using the array index notation (like c[6]), mongo first calls the cursor.toArray() method and loads into RAM all documents returned by the cursor. The index is then applied to the resulting array. This operation iterates the cursor completely and exhausts the cursor. For very large result sets, mongo may run out of available memory.
find()方法返回一个cursor,如果调用cursor[1],则会隐式调用cursor.toArray()函数,如果结果集很大,可能会OOM
With the findOne() method you can return a single document from a MongoDB collection. The findOne() method takes the same parameters as find(), but returns a document rather than a cursor.
findOne()方法确实地返回一个文档,而不是一个cursor
Collections are analogous to a table in relational databases.
芒果的collection是松散组织(no schema),允许插入完全没有关系的多条数据,唯一的关联是共享_id索引。但是这是不好的做法,还是推荐将有组织的文档插入同一个集合
Read 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 the fields from the matching documents to return. The projection limits the amount of data that MongoDB returns to the client over the network.
芒果的查询语句,包括criteria、projection、cursor modifier
MongoDB 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.
•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 (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 projections have the following properties:
•In MongoDB, the _id field is always included in results unless explicitly excluded.
•For fields 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.
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 to 20 times to print up to the first 20 documents in the results.
在mongo shell中,如果find()方法没有赋值给一个var变量,则会默认取出前20条记录(调用20次cursor.next)
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.
cursor并不是isolated的,并不是DB某个时刻的快照
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.
在合适的情况下,建立索引可以有效地提升查询效率;但是索引对写效率有影响,所以:
1、找出最频繁的,成为瓶颈的查询语句,在列上建立索引
2、避免创建毫无意义的索引
3、在业务负载低时进行索引创建的操作
Example:
Given a collection inventory with the following index on the type and item fields:
{ type: 1, item: 1 }
This index will cover the following query on the type and item fields, which returns only the item field:
db.inventory.find( { type: "food", item:/^c/ },
{ item: 1, _id: 0 } )
However, the index will not cover the following query, which returns the item field and the _id field:
db.inventory.find( { type: "food", item:/^c/ },
{ item: 1 } )
The MongoDB query optimizer processes queries and chooses the most efficient query plan for a query given the available 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.
每个mongod是一个实例,mongod只是启动的入口,可以只有一个。但是指定了dbpath之后,会生成mongod.lock文件,阻止其他实例访问。同一个目录下的mongod.exe(.sh)可以启动多个实例,只要指定了不同的dbpath
Read operations to a sharded cluster. Query criteria includes the shard key. The query router mongos can target 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 scatter gather queries can be (very) inefficient. On larger clusters, scatter gather queries are unfeasible for routine operations.
虽然mongodb做sharding很容易,但是分片仍然需要提前规划好。比如有3个sharding mongod,共享一个shard key。那么当查询criteria包含这个key时, mongos立刻可以知道应该到哪台mongod上查询文档,查询的效率很高;反之如果查询criteria不包含shard key,mongos就需要轮询所有的mongod,这称为scatter gather,查询效率非常低,应该尽量避免
Replica sets use read preferences to determine where and how to route read operations to members of the replica set. By default, MongoDB always reads data from a replica set’s primary. You can modify that behavior by changing the read preference mode.
A 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.
No insert, update, or remove can affect more than one document atomically.
有且只有针对单个文档的写操作是atom的
If you add a new document without the _id field, the client library or the mongod instance adds an _id field and populates the field with a unique ObjectId.
If you specify the _id field, the value must be unique within the collection. For operations with write concern, if you try to create a document with a duplicate _id value, mongod returns a duplicate key exception.
如果手工生成_id,必须保证是唯一的
MongoDB provides different levels of write concern to better address the specific needs of applications. Clients may adjust write concern to ensure that the most important operations persist successfully to an entire MongoDB deployment. For other less critical operations, clients can adjust the write concern to ensure faster performance rather than ensure persistence to the entire deployment.
Although the errors ignored write concern provides fast performance, this performance gain comes at the cost of significant risks for data persistence and durability.
In-place-updates are significantly more efficient than updates that cause document growth. When possible, use data models that minimize the need for document growth.
in-place更新效率比较高,如果改变了文档大小,mongo可能需要先分配磁盘空间,从而降低更新效率
Solid state drives (SSDs) can outperform spinning hard disks (HDDs) by 100 times or more for random workloads.
The insert() method, when passed an array of documents, will perform a bulk insert, and inserts each document atomically. Drivers provide their own interface for this kind of operation.
如果传递给insert()方法一个[]参数,则会执行batch insert,效率比较高
Update operations can increase the size of the document. If a document outgrows its current allocated record space, MongoDB must allocate a new space and move the document to this new location.
To reduce the number of moves, MongoDB includes a small amount of extra space, or padding, when allocating the record space. This padding reduces the likelihood that a slight increase in document size will cause the document to exceed its allocated record size.
db.testData.find({$or:[{name:"zhengshengdong",age:29},{age:22}]})
db.testData.find({age:29,$or:[{name:"zhengshengdong"},{name:"chenruizhong"}]})
If you do not specify a query, remove() removes all documents from a collection, but does not remove the indexes. To remove all documents from a collection, it may be more efficient to use the drop() method to drop the entire collection, including the indexes, and then recreate the collection and rebuild the indexes.
如果系统已经超过了其负载,那么移动数据会变得很难,不要在系统性能不行的时候才去加shard。为了分片,mongo需要将待移动数据放到RAM中,如果RAM不够,则业务查询所需的数据会被移出RAM,性能会越来越差
分享到:
相关推荐
MongoDB是一种分布式文档数据库,是NoSQL数据库的一种,它以其灵活性、高性能和可扩展性而备受推崇。在本文中,我们将深入探讨MongoDB的核心概念、功能特性以及如何利用其进行项目开发。 首先,MongoDB的核心概念...
### 大数据背景下MongoDB数据库档案文档存储去重技术研究 #### 摘要与背景 随着信息技术的迅速发展,特别是在大数据时代,档案文档的管理面临着前所未然的挑战。传统档案存储方式通常采用文件系统结合关系数据库的...
本系统是本人初学MongoDb时所写,代码不是很完美,基本实现图书管理系统的增删改查等基本功能,目前尚有一处缺陷未解决——在查询后只能在控制台看到结果,没有反馈到界面上,有兴趣的朋友可以加以修改,相信这是一...
MongoDB采用了数据库(database)、集合(collection)、文档对象(document)的三层结构。其中: - 数据库:存储相关集合的空间。 - 集合:类似于关系型数据库中的表,但没有固定的结构。 - 文档对象:每个集合中的基本...
MongoDB作为一种非关系型数据库,基于文档存储模型,非常适合用来存储和管理微服务产生的各种日志数据。它的特点包括: - **灵活性**:能够适应不断变化的数据结构,这对于日志数据来说尤为重要,因为日志格式可能会...
4. **数据转换**:MongoDB中的数据模型是基于文档的,每个文档都是一组键值对。因此,我们需要将表格数据转换为JSON-like格式。例如,创建一个文档,其中包含“age”,“race”,“gender”等键,以及对应的统计值。...
MongoDB 是一个流行的开源文档型数据库系统,以其灵活性和高性能而受到广泛青睐。在处理数据库问题时,诊断是关键步骤,尤其是对于性能调优和故障排除。以下是对MongoDB数据库问题诊断的一些关键知识点: 1. **性能...
本文将根据文档内容,对Hutool中的各个组件进行详细的介绍。 核心部分(Hutool-core)包含了多个工具类,它们覆盖了克隆、类型转换、日期时间处理、IO流操作、资源访问、字符串处理、加密解密等各个方面,从而满足...
**MongoDB文档数据库** MongoDB是一种NoSQL数据库,适合处理大规模非结构化或半结构化数据。在论文数据采集项目中,MongoDB可能用于存储那些无法直接映射到传统关系表的数据,如全文内容、作者简介等。MongoDB的...
以下内容假定API服务器在本地运行,并且已成功连接到MongoDB。 列出所有数据库的名称 curl "http://127.0.0.1:12345/databases" 列出数据库中的所有“概要文件”收集文档 curl ...
对于软件类项目,可能会涉及到编程语言(如Java、Python、C++等)、框架(如Spring Boot、React、Vue等)、数据库管理系统(如MySQL、Oracle、MongoDB等)的应用。 4. **测试报告**:在系统实现后,需要进行功能...
1. **安装与连接**:首先,你需要在Jupyter Notebook环境中安装`pymongo`库,这是Python官方支持的MongoDB驱动程序。通过运行`!pip install pymongo`来安装。然后,你可以使用`pymongo`创建MongoDB客户端并连接到你...
3. **数据库设计**:系统采用了某种类型的数据库(可能是MySQL、MongoDB等),并提供了数据库关系图和脚本,帮助理解数据表之间的关系和初始数据的填充。数据库设计对于存储和检索宿舍管理中的各种信息(如宿舍信息...
这需要我们具备一定的科研写作能力,明确论文的结构(如摘要、引言、方法、结果、讨论、结论等部分),并遵循学术规范,正确引用参考文献。同时,要能够将技术概念、设计思路和实验结果以清晰、逻辑的方式呈现出来。...
- 结合 TextRank 算法生成文档摘要。 #### 案例二:智能问答系统 - **目标**:开发一个基于高考政治知识点的智能问答系统,帮助学生解答疑惑。 - **技术方案**: - 首先使用文档处理技术提取知识点。 - 基于抽取...
4. **UI设计**:用户界面应该简洁易用,能够清晰展示订阅源列表、文章标题、摘要以及链接。可以采用响应式设计,确保在不同设备上都能良好显示。 5. **数据存储**:用户订阅的信息和阅读状态需要持久化存储。可以...
- **文本摘要**:通过对文档内容进行分析,可以自动抽取关键信息,形成文档摘要。这对于快速了解文档内容非常有用。 - **情感分析**:通过分析文本中的词汇和句子结构,可以判断出作者的情感倾向,如正面、负面或...
- **文本摘要**: IT领域可以利用算法自动生成文档摘要,帮助用户快速了解文档主要内容。 ### 3. 数据存储与管理 - **数据库设计**: 存储关于文档的信息,比如标题、描述、标签等,可以使用关系型数据库(如MySQL)...
MongoDB是一个高性能、无模式的文档型数据库,适合处理JSON格式的数据。在博客系统中,我们可能需要存储用户信息、文章、评论等数据。MongoDB提供了丰富的查询API,使得数据操作变得简单。 1. 数据模型:在MongoDB...
提取香港超市价格数据并将其转储到MongoDB中的脚本 从获取数据 例子 这就是示例文档的外观。 每个文档都存储产品信息,并维护一个价格对象阵列,这些价格对象是在每次获取数据时生成的。 { "_id" : "P000000038",...