在我的MongoDB中,我有一个学生集合,其中有10条记录,字段为name和roll。这个收藏的一个记录是:

{
    "_id" : ObjectId("53d9feff55d6b4dd1171dd9e"),
    "name" : "Swati",
    "roll" : "80",
}

我想检索字段滚动仅为集合中的所有10条记录,因为我们会在传统数据库中使用:

SELECT roll FROM student

我浏览了很多博客,但所有的结果都是一个必须有WHERE子句的查询,例如:

db.students.find({ "roll": { $gt: 70 })

查询等价于:

SELECT * FROM student WHERE roll > 70

我的要求是只找到一个钥匙,不附带任何条件。它的查询操作是什么。


当前回答

单次更新: db.collection_name。更新({field_name_1:(“价值”)},{$设置:{field_name_2:“new_value}});

对于MultiUpdate: db.collection_name。updateMany ({field_name_1:(“价值”)},{$设置:{field_name_2:“new_value}});

确保索引正确。

其他回答

这对我很有用,

db.student.find({},{"roll":1})

where子句中没有条件,即在第一个花括号内。 下一个花括号内:结果中需要的投影字段名称列表,1表示特定字段是查询结果的一部分

db.<collection>.find({}, {field1: <value>, field2: <value> ...})

在你的例子中,你可以这样做:

db.students.find({}, {"roll":true, "_id":false})

投影

类型中返回哪些字段 匹配的文件。类型的文档为参数 下面的形式:

{ field1: <value>, field2: <value> ... }

<值>可以是以下任意值: 1或true表示在返回文档中包含该字段。 0或false排除该字段。

NOTE

对于_id字段,您不必显式地指定_id: 1到 返回_id字段。find()方法总是返回_id字段 除非指定_id: 0来屏蔽该字段。

阅读更多

如果你在NodeJs中使用MongoDB驱动程序,那么上述答案可能不适合你。您将不得不执行类似的操作,以仅获得选定的属性作为响应。

import { MongoClient } from "mongodb";

// Replace the uri string with your MongoDB deployment's connection string.
const uri = "<connection string uri>";
const client = new MongoClient(uri);

async function run() {
  try {
    await client.connect();
    const database = client.db("sample_mflix");
    const movies = database.collection("movies");

    // Query for a movie that has the title 'The Room'
    const query = { title: "The Room" };

    const options = {
      // sort matched documents in descending order by rating
      sort: { "imdb.rating": -1 },
      // Include only the `title` and `imdb` fields in the returned document
      projection: { _id: 0, title: 1, imdb: 1 },
    };

    const movie = await movies.findOne(query, options);

    /** since this method returns the matched document, not a cursor, 
     * print it directly 
    */
    console.log(movie);
  } finally {
    await client.close();
  }
}

run().catch(console.dir);

这段代码是从实际的MongoDB文档中复制的,你可以在这里查看。 https://docs.mongodb.com/drivers/node/current/usage-examples/findOne/

从表中获取所有数据

db.student.find({})

SELECT * FROM student


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

db.student.find({}, {_id:0})

SELECT name, roll FROM student


从_id字段中获取所有数据

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

SELECT id, roll FROM student


从一个没有_id的字段中获取所有数据

db.student.find({}, {roll:1, _id:0})

从学生中选择卷


使用where子句查找指定的数据

db.student.find({roll: 80})

SELECT * FROM student WHERE roll = '80'


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

db.student.find({ "roll": { $gt: 70 }}) // $gt is greater than 

SELECT * FROM student WHERE roll > '70'


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

db.student.find({ "roll": { $gte: 70 }}) // $gte is greater than or equal

SELECT * FROM student WHERE >= '70'


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

db.student.find({ "roll": { $lte: 70 }}) // $lte is less than or equal

SELECT * FROM student WHERE roll <= '70'


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

db.student.find({ "roll": { $lt: 70 }})  // $lt is less than

SELECT student WHERE roll < '70'

如果你只想为集合中的所有10条记录检索字段“roll”。 那就试试这个。

在MongoDb中:

db.students。Find ({}, {" roll ": {" $roll "})

Sql:

从学生中选择名单