栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Field Projection中忽略batchSize字段名称

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Field Projection中忽略batchSize字段名称

您正确地认为驱动程序将其错误地解释为

batchSize
选项,而忽略了projection语句。

尽管在现代驱动程序版本中,执行此操作的正确方法是实际使用

.project()
“游标方法”。这与其他语言驱动程序的实现更加一致。

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

作为完整的演示:

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()  }})()

产生输出:

[  {    "_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": []  }]

如果第一个输出形式是更正的形式,则使用

.project()



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/436451.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号