gpt4 book ai didi

node.js - MongoDB Node.js 查找 API 调用如何知道您是否将字段或选项作为参数传递?

转载 作者:太空宇宙 更新时间:2023-11-04 01:04:45 25 4
gpt4 key购买 nike

MongoDB Node.js find API call允许选择器、字段、回调?选择器、选项、回调?。字段和选项对象之间有何不同?它们都可以是对象吗?

最佳答案

您可以在 source code 中亲自查看.

该算法非常简单:迭代每个提供的值并检查它是否是已知选项。如果它不是一个选项,那么它一定是一个字段。

var testForFields = {
limit: 1, sort: 1, fields:1, skip: 1, hint: 1, explain: 1, snapshot: 1, timeout: 1, tailable: 1, tailableRetryInterval: 1
, numberOfRetries: 1, awaitdata: 1, exhaust: 1, batchSize: 1, returnKey: 1, maxScan: 1, min: 1, max: 1, showDiskLoc: 1
, comment: 1, raw: 1, readPreference: 1, partial: 1, read: 1, dbName: 1, oplogReplay: 1, connection: 1
};

再往下,在 find 方法中:

if(len === 2 && !Array.isArray(fields)) {
var fieldKeys = Object.getOwnPropertyNames(fields);
var is_option = false;

for(var i = 0; i < fieldKeys.length; i++) {
if(testForFields[fieldKeys[i]] != null) {
is_option = true;
break;
}
}

if(is_option) {
options = fields;
fields = undefined;
} else {
options = {};
}
}

关于node.js - MongoDB Node.js 查找 API 调用如何知道您是否将字段或选项作为参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24179661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com