gpt4 book ai didi

node.js - Mongoose Model.find().select() 如果Select是空字符串返回什么?

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:14 24 4
gpt4 key购买 nike

假设我们使用的是一个简单的架构:

const phoneRegex = require('./phoneRegex');
const Schema = require('mongoose').Schema;
const PersonSchema = new Schema({
firstName: String,
lastName: String,
phone: {
type: String,
regex: phoneRegex,
},
Address: {
street1: String,
street2: String,
city: String,
state: String,
zip: String,
}
});

module.exports = Person = require('mongoose').model('Person', PersonSchema);

我想要对 mongoose 进行预设调用以查找人员列表,但我希望能够选择限制从数据库返回的内容。我知道我可以使用模型的 select() 函数,但是当您提供空字符串时会发生什么? (即 Person.find(options).select('');)

我的预定义函数如下所示:

//sample options object.
const options = {
firstName: {
$regex: '^John',
$options: 'i',
},
};
const Person = require('./person');
const fetchAll = (Options, select = '') => {
return Person.find(Options).select(select);
};

我知道我想传递属性名称来选择它返回,但是空选择字符串与“SELECT NONE”相同吗?

最佳答案

读完the docs后看起来除了 _id 字段之外的所有字段都将被忽略,因为默认情况下包含该字段。您的意思是不包含任何内容,排除其他所有内容

我目前没有 Mongoose 设置来测试这一点,所以这更多的是一个有根据的猜测。

关于node.js - Mongoose Model.find().select() 如果Select是空字符串返回什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56306235/

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