簡體   English   中英

Mongoose,使用查找選擇特定字段

[英]Mongoose, Select a specific field with find

我試圖只選擇一個特定的字段

exports.someValue = function(req, res, next) {
    //query with mongoose
    var query = dbSchemas.SomeValue.find({}).select('name');

    query.exec(function (err, someValue) {
        if (err) return next(err);
        res.send(someValue);
    });
};

但是在我的 json 響應中,我也收到了 _id,我的文檔架構只有兩個字段,_id 和 name

[{"_id":70672,"name":"SOME VALUE 1"},{"_id":71327,"name":"SOME VALUE 2"}]

為什么???

_id字段始終存在,除非您明確排除它。 使用-語法這樣做:

exports.someValue = function(req, res, next) {
    //query with mongoose
    var query = dbSchemas.SomeValue.find({}).select('name -_id');

    query.exec(function (err, someValue) {
        if (err) return next(err);
        res.send(someValue);
    });
};

或者明確地通過一個對象:

exports.someValue = function(req, res, next) {
    //query with mongoose
    var query = dbSchemas.SomeValue.find({}).select({ "name": 1, "_id": 0});

    query.exec(function (err, someValue) {
        if (err) return next(err);
        res.send(someValue);
    });
};

現在有一種更短的方法可以做到這一點:

exports.someValue = function(req, res, next) {
    //query with mongoose
    dbSchemas.SomeValue.find({}, 'name', function(err, someValue){
      if(err) return next(err);
      res.send(someValue);
    });
    //this eliminates the .select() and .exec() methods
};

如果您需要大多數Schema fields並且只想省略少數幾個,您可以在字段name加上- 例如,第二個參數中的"-name"包括文檔中的name字段,而此處給出的示例將在返回的文檔中包含name字段。

使用 Mongoose 中的本機 MongoDB 代碼有更好的方法來處理它。

exports.getUsers = function(req, res, next) {

    var usersProjection = { 
        __v: false,
        _id: false
    };

    User.find({}, usersProjection, function (err, users) {
        if (err) return next(err);
        res.json(users);
    });    
}

http://docs.mongodb.org/manual/reference/method/db.collection.find/

筆記:

變量用戶投影

此處列出的對象列表將不會返回/打印。

數據庫數據

[
  {
    "_id": "70001",
    "name": "peter"
  },
  {
    "_id": "70002",
    "name": "john"
  },
  {
    "_id": "70003",
    "name": "joseph"
  }
]

詢問

db.collection.find({},
{
  "_id": 0,
  "name": 1
}).exec((Result)=>{
    console.log(Result);
})

輸出:

[
  {
    "name": "peter"
  },
  {
    "name": "john"
  },
  {
    "name": "joseph"
  }
]

工作示例游樂場

關聯

提示: 0 表示忽略,1 表示顯示。

示例 1:

User.find({}, { createdAt: 0, updatedAt: 0, isActive: 0, _id : 1 }).then(...)

示例 2:

User.findById(id).select("_id, isActive").then(...)

示例 3:

User.findById(id).select({ _id: 1, isActive: 1, name: 1, createdAt: 0 }).then(...)

排除

下面的代碼將檢索每個文檔中除密碼以外的所有字段:

const users = await UserModel.find({}, {
  password: 0 
});
console.log(users);

輸出

[
  {
    "_id": "5dd3fb12b40da214026e0658",
    "email": "example@example.com"
  }
]

包括

下面的代碼只會檢索每個文檔中的電子郵件字段:

const users = await UserModel.find({}, {
  email: 1
});
console.log(users);

輸出

[
  {
    "email": "example@example.com"
  }
]

准確的方法是將.project()游標方法與新的mongodbnodejs驅動程序一起使用。

var query = await dbSchemas.SomeValue.find({}).project({ name: 1, _id: 0 })

我在 mongoose 中找到了一個非常好的選擇,它使用不同的返回數組,所有文檔中的特定字段。

User.find({}).distinct('email').then((err, emails) => { // do something })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM