在 MongoDB 集合中创建索引?
要创建索引,请在 MongoDB 中使用 createIndex()。我们创建一个带有文档的集合 −
> db.demo702.createIndex({"details.id":1});
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.demo702.insertOne({
... "details" : [
... {
... id:101,
... studentInfo:{
... "StudentName" : "Chris",
... "StudentAge" : 23,
... }
... },
... {
...
... id: 102,
... studentInfo:{
... "StudentName" : "Robert",
... "StudentAge" : 20,
... }
... }
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea6ea3b551299a9f98c93b3")
}使用 find() 方法显示集合中的所有文档 −
> db.demo702.find().pretty();
这将产生以下输出 −
{
"_id" : ObjectId("5ea6ea3b551299a9f98c93b3"),
"details" : [
{
"id" : 101,
"studentInfo" : {
"StudentName" : "Chris",
"StudentAge" : 23
}
},
{
"id" : 102,
"studentInfo" : {
"StudentName" : "Robert",
"StudentAge" : 20
}
}
]
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP