字段投影中忽略的 batchSize 字段名称 [英] batchSize field name ignored in Field Projection

查看:35
本文介绍了字段投影中忽略的 batchSize 字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 user_batch 集合.它包含以下文件:

<代码>[{_id: ObjectId("594baf96256597ec035df23c"),名称:第 1 批",批量大小:30,用户:[]},{_id: ObjectId("594baf96256597ec035df234"),名称:第 2 批",批量大小:50,用户:[]}]

在查找查询中,我只想投影 namebatchSize.但是当我从 nodejs 执行 find 查询时,我在查询结果中获取了整个文档.查询:

db.collection('user_batch').find({}, {name: 1, batchSize: 1}).toArray((err, result) => {如果(错误)控制台日志(错误)别的控制台日志(结果)})

如果我只是通过 {name: 1} 那么它会投射 _id 和 name.但是如果我通过 batchSize 那么它会返回整个文档.

注意:在 Mongo Shell 中执行此查询时我没有遇到此问题

解决方案

您是正确的,驱动程序错误地将此解释为 batchSize 选项并忽略了投影语句.

尽管在现代驱动程序版本中执行此操作的正确方法是实际使用 .project() 光标方法".这与其他语言驱动程序实现更加一致.

 db.collection('collection').find().project({名称:1,batchSize:1}).toArray();

作为一个完整的演示:

const mongodb = require('mongodb'),MongoClient = mongodb.MongoClient;(异步函数(){让分贝;尝试 {db = await MongoClient.connect('mongodb://localhost/test');//新表单使用 .project() 作为游标方法让结果 = 等待 db.collection('collection').find().project({名称:1,batchSize:1}).toArray();console.log(JSON.stringify(result,undefined,2));//传统形式将此混淆为传统的光标选项"让其他 = 等待 db.collection('collection').find({},{ name: 1, batchSize: 1 }).toArray();console.log(JSON.stringify(other,undefined,2));}赶上(e){控制台错误(e)} 最后 {db.close()}})()

产生输出:

<预><代码>[{"_id": "594baf96256597ec035df23c","name": "第 1 批",批量大小":30},{"_id": "594baf96256597ec035df234","name": "第 2 批",批量大小":50}][{"_id": "594baf96256597ec035df23c","name": "第 1 批",批量大小":30,用户":[]},{"_id": "594baf96256597ec035df234","name": "第 2 批",批量大小":50,用户":[]}]

其中第一个输出形式是更正的,使用 .project()

I have a user_batch collection. It contains following documents:

[{
  _id: ObjectId("594baf96256597ec035df23c"),
  name: "Batch 1",
  batchSize: 30,
  users:[]
 },
 {
  _id: ObjectId("594baf96256597ec035df234"),
  name: "Batch 2",
  batchSize: 50,
  users:[]
 }]

In find query I want to project only name and batchSize. But when I execute find query from nodejs, I'm getting entire document in query result. Query:

db.collection('user_batch').find({}, {name: 1, batchSize: 1}).toArray((err, result) => {
  if(err) 
    console.log(err)
  else
    console.log(result)
})

If I just pass {name: 1} then it will project _id and name. But if I pass batchSize then it will return entire document.

Note: I'm not facing this issue while executing this query in Mongo Shell

解决方案

You are correct that the driver incorrectly interprets this as the batchSize option and ignores the projection statement.

The correct way to do this though in modern driver releases is to actually use the .project() "cursor method" instead. This is more consistent with other language driver implementations.

    db.collection('collection').find()
      .project({ name: 1, batchSize: 1})
      .toArray();

As a full demonstration:

const mongodb = require('mongodb'),
      MongoClient = mongodb.MongoClient;


(async function() {

  let db;

  try {
    db = await MongoClient.connect('mongodb://localhost/test');

    // New form uses .project() as a cursor method
    let result = await db.collection('collection').find()
      .project({ name: 1, batchSize: 1})
      .toArray();

    console.log(JSON.stringify(result,undefined,2));

    // Legacy form confuses this as being a legacy "cursor option"
    let other = await db.collection('collection')
      .find({},{ name: 1, batchSize: 1 })
      .toArray();

    console.log(JSON.stringify(other,undefined,2));

  } catch(e) {
    console.error(e)
  } finally {
    db.close()
  }

})()

Produces the output:

[
  {
    "_id": "594baf96256597ec035df23c",
    "name": "Batch 1",
    "batchSize": 30
  },
  {
    "_id": "594baf96256597ec035df234",
    "name": "Batch 2",
    "batchSize": 50
  }
]
[
  {
    "_id": "594baf96256597ec035df23c",
    "name": "Batch 1",
    "batchSize": 30,
    "users": []
  },
  {
    "_id": "594baf96256597ec035df234",
    "name": "Batch 2",
    "batchSize": 50,
    "users": []
  }
]

Where the first output form is the corrected one, using .project()

这篇关于字段投影中忽略的 batchSize 字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆