如何从 MongoDB 集合中读取特定的键值对?
您可以使用 dot(.) 符号来读取 MongoDB 集合中某个特定的键值对。我们首先创建一个带有文档的集合 -
> db.readSpecificKeyValueDemo.insertOne(
... {
... "_id": 100,
... "StudentDetails":
... {
... "StudentFirstName" : "David",
... "StudentLastName" :"Miller",
... "StudentAge":23,
... "StudentCountryName":"US"
... }
... }
... );
{ "acknowledged" : true, "insertedId" : 100 }以下查询可借助 find() 方法显示集合中的所有文档 -
> db.readSpecificKeyValueDemo.find().pretty();
这会产生以下输出 -
{
"_id" : 100,
"StudentDetails" : {
"StudentFirstName" : "David",
"StudentLastName" : "Miller",
"StudentAge" : 23,
"StudentCountryName" : "US"
}
}以下查询可从 MongoDB 集合中读取某个特定的键值对 -
> db.readSpecificKeyValueDemo.find({},{"StudentDetails.StudentCountryName":1}).pretty();这会产生以下输出 -
{ "_id" : 100, "StudentDetails" : { "StudentCountryName" : "US" } }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP