MongoDB 是否能在不影响性能的情况下进行大量插入和更新?
使用 insertMany() 将多条文档插入到集合中。通过此操作,可以使用 ensureIndex() 提升性能。
让我们创建一个包含文档的集合并插入多条文档 −
> db.demo325.insertMany( [
... { _id: 101, Name: "Chris", Age: 23 },
... { _id: 102, Name: "David", Age: 24 },
... { _id: 103, Name: "Bob", Age: 22 }
... ] );
{ "acknowledged" : true, "insertedIds" : [ 101, 102, 103 ] }使用 find() 方法显示集合中的所有文档 −
> db.demo325.find().pretty();
这将生成以下输出 −
{ "_id" : 101, "Name" : "Chris", "Age" : 23 }
{ "_id" : 102, "Name" : "David", "Age" : 24 }
{ "_id" : 103, "Name" : "Bob", "Age" : 22 }使用 ensureIndex() −
> db.demo325.ensureIndex({Name:1});这将生成以下输出 −
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP