如何为MongoDB集合中的所有文档选择单个字段?

如何为MongoDB集合中的所有文档选择单个字段?,mongodb,projection,mongo-collection,Mongodb,Projection,Mongo Collection,在我的MongoDB中,我有一个包含10条记录的学生集合,这些记录包含字段name和roll。此集合的一个记录是: { "_id" : ObjectId("53d9feff55d6b4dd1171dd9e"), "name" : "Swati", "roll" : "80", } 我只想检索集合中所有10条记录的字段roll,就像我们在传统数据库中使用的那样: SELECT roll FROM student 我浏览了许多博客,但所有的博客都产生了一个查询,其中必须包

在我的MongoDB中,我有一个包含10条记录的学生集合,这些记录包含字段
name
roll
。此集合的一个记录是:

{
    "_id" : ObjectId("53d9feff55d6b4dd1171dd9e"),
    "name" : "Swati",
    "roll" : "80",
}
我只想检索集合中所有10条记录的字段
roll
,就像我们在传统数据库中使用的那样:

SELECT roll FROM student
我浏览了许多博客,但所有的博客都产生了一个查询,其中必须包含
WHERE
子句,例如:

db.students.find({ "roll": { $gt: 70 })
该查询相当于:

SELECT * FROM student WHERE roll > 70
我的要求是在没有任何条件的情况下只找到一把钥匙。那么,该操作的查询操作是什么。

来自:

投影可以显式地包含多个字段。在下面的操作中,
find()
方法返回与查询匹配的所有文档。在结果集中,匹配文档中仅返回物料和数量字段,默认情况下,返回_id字段

db.inventory.find({type:'food'},{item:1,qty:1})

在本例中,Mongo的员工返回的文档将只包含
项目
数量
\u id
字段


因此,您应该能够发布如下声明:

db.students.find({}, {roll:1, _id:0})
上述语句将选择students集合中的所有文档,返回的文档将仅返回
roll
字段(并排除
\u id


如果我们不提及
\u id:0
,返回的字段将是
roll
\u id
。默认情况下始终显示“\u id”字段。因此,我们需要明确提到
\u id:0
以及
roll

我认为mattingly890的答案是正确的,下面是另一个示例以及模式/commmand

db.collection.find({},{您的\u键:1,\u id:0})


仅出于教育目的,您还可以通过以下任一方式进行:

一,

二,


`

尝试以下查询:

db.student.find({}, {roll: 1, _id: 0}).pretty();
希望这有帮助

db.student.find({}, {"roll":1, "_id":0})
这相当于-

从学生中选择roll



find({},{“roll”:1,“name”:1,“\u id”:0})

这相当于-

选择roll,学生姓名

正在获取学生的姓名

获取学生的姓名和名单


在shell中使用如下查询:

1.使用
数据库名称

e.g: use database_name
2.当匹配时仅返回特定字段信息,
\u id:0
指定不在结果中显示id

db.collection_name.find( { "Search_Field": "value" }, 
                  { "Field_to_display": 1,_id:0 }  )

在MongoDB3.4中,我们可以使用以下逻辑,我不确定以前的版本

选择roll from student==>db.student.find(!{},{roll:1})


上述逻辑有助于定义某些列(如果它们较少)

从表中获取所有数据

db.student.find({})
db.student.find({roll: 80})
从学生中选择*



从没有id的表中获取所有数据

db.student.find({}, {_id:0})
db.student.find({}, {roll:1})
db.student.find({}, {roll:1, _id:0})
选择名称,从学生中滚动


从一个id为的字段获取所有数据

db.student.find({}, {_id:0})
db.student.find({}, {roll:1})
db.student.find({}, {roll:1, _id:0})
选择id,从学生中滚动


从一个字段中获取所有数据,但不带id

db.student.find({}, {_id:0})
db.student.find({}, {roll:1})
db.student.find({}, {roll:1, _id:0})
从学生中选择roll


使用where子句查找指定数据

db.student.find({})
db.student.find({roll: 80})
从滚动=80的学生中选择*


使用where子句和大于条件查找数据

db.student.find({ "roll": { $gt: 70 }}) // $gt is greater than 
db.student.find({ "roll": { $gte: 70 }}) // $gte is greater than or equal
db.student.find({ "roll": { $lte: 70 }}) // $lte is less than or equal
db.student.find({ "roll": { $lt: 70 }})  // $lt is less than
选择*从学生卷>'70'


使用where子句和大于或等于条件查找数据

db.student.find({ "roll": { $gt: 70 }}) // $gt is greater than 
db.student.find({ "roll": { $gte: 70 }}) // $gte is greater than or equal
db.student.find({ "roll": { $lte: 70 }}) // $lte is less than or equal
db.student.find({ "roll": { $lt: 70 }})  // $lt is less than
从学生中选择*滚动>='70'


使用where子句和小于或等于条件查找数据

db.student.find({ "roll": { $gt: 70 }}) // $gt is greater than 
db.student.find({ "roll": { $gte: 70 }}) // $gte is greater than or equal
db.student.find({ "roll": { $lte: 70 }}) // $lte is less than or equal
db.student.find({ "roll": { $lt: 70 }})  // $lt is less than
从学生中选择*滚动这对我有效

db.student.find({},{"roll":1})
where子句中没有条件,即第一个花括号内。
在下一个大括号内:结果中需要的投影字段名称列表,如果您只想检索集合中所有10条记录的字段“roll”,则1表示特定字段是查询结果的一部分。 那就试试这个

在MongoDb中:

db.students.find({},{“roll”:{“$roll})

在Sql中:

从学生中选择卷


为了更好地理解,我编写了类似的MySQL查询。

Selecting specific fields 
MongoDB:db.collection\u name.find({},{name:true,email:true,phone:true})

MySQL:从表\u name中选择姓名、电子邮件、电话

MongoDB:db.collection\u name.find({email:'you@email.com{name:true,email:true,phone:true})

MySQL:从电子邮件地址所在的表\u name中选择姓名、电子邮件、电话you@email.com",


给你,三种方法,最短到无聊:

db.student.find({}, 'roll _id'); // <--- Just multiple fields name space separated
// OR
db.student.find({}).select('roll _id'); // <--- Just multiple fields name space separated
// OR
db.student.find({}, {'roll' : 1 , '_id' : 1 ); // <---- Old lengthy boring way
投影

db.student.find({ "roll": { $gt: 70 }}) // $gt is greater than 
db.student.find({ "roll": { $gte: 70 }}) // $gte is greater than or equal
db.student.find({ "roll": { $lte: 70 }}) // $lte is less than or equal
db.student.find({ "roll": { $lt: 70 }})  // $lt is less than
projection参数确定在中返回哪些字段 匹配文档。投影参数获取 以下表格:

{field1:,field2:…}
可以是以下任意一种:
  • 1或true,将该字段包括在退货文档中

  • 0或false以排除该字段

  • 注意

    对于_id字段,不必显式指定_id:1 返回_id字段。find()方法始终返回_id字段 除非指定_id:0以抑制该字段

    虽然已完成,但值得注意的是,这些命令在API上可能会有所不同(对于不使用mongo shell的命令)。
    有关详细信息,请参阅

    例如,Nodejs有一个名为“projection”的方法,可以附加到find函数中进行投影

    按照相同的示例集,类似以下的命令可用于节点:

    db.student.find({}).project({roll:1})

    选择_id,从学生中滚动


    db.student.find({}).pro