如何使我的集合索引使用复合多键索引?
为此,请使用 ensureIndex()。让我们创建一个带有文档的集合 −
> db.demo678.ensureIndex({id:1,"details.userId":1});
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.demo678.insertOne(
... {
... id:101,
...
... "details" : [
... {
... "userId" : "1001",
... "userName":"Chris"
... },
... {
... "userId" : "1002",
... "userName":"David"
... }
... ],
... "otherDetails" : [
... {
... CountryName:"US",
... EmailId:["Chris@gmail.com","David@gmail.com"]
... }
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea4276904263e90dac943fc")
}借助 find() 方法显示集合中的所有文档 −
> db.demo678.find();
这将产生以下输出 −
{ "_id" : ObjectId("5ea4276904263e90dac943fc"), "id" : 101, "details" : [
{ "userId" : "1001", "userName" : "Chris" },
{ "userId" : "1002", "userName" : "David" }
], "otherDetails" : [
{ "CountryName" : "US", "EmailId" : [ "Chris@gmail.com", "David@gmail.com" ] }
] }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP