【发布时间】:2018-03-09 10:49:18
【问题描述】:
我在查询中访问 user.info.name.familyName 时遇到问题。我对数据库设计和查询很陌生,我不知道它是否可能。
这就是我的架构。也许它可能需要针对我正在努力做的工作进行重新设计。
var UserSchema = mongoose.Schema({
username: {
type: String,
index: true,
required: true,
unique: true
},
password: {
type: String,
required: true
},
email: {
type: String,
required: true,
lowercase: true
},
emailVerified: {
type: Boolean,
default: false
},
info: {
name: {
givenName: String,
familyName: String,
},
address: {
street: String,
city: String,
state: String,
zipCode: String
},
primaryPhone: String,
cellPhone: String,
website: String,
licenseNumber: String,
title: String,
company: String
},
avatar: {
type: String,
default: '/images/avatar/default/contacts-128.png'
},
friends: [{ type: ObjectId, ref: User }],
friendRequests: [{ type: ObjectId, ref: User }]
});
我有一个搜索功能,目前按用户名或电子邮件搜索并且工作正常,但我希望它也可以按用户给定名称或家庭名称进行搜索。这里是搜索功能...
module.exports.search = function(searchValue, callback) {
var searchValue = new RegExp(`^${searchValue}`, 'i');
var query = {
$or: [
{username: searchValue},
{email: searchValue}
// {givenName condition here...}
]
}
User.find(query, callback).limit(10);
}
我试过这个......但它没有用
{info.name.givenName: searchValue}
我尝试了其他一些无意义的事情,但它们也失败了......
总结一下我的问题,我将如何重组架构或查询以便能够对照 user.info.name.givenName 检查 searchValue?
【问题讨论】:
标签: node.js mongodb mongoose mongodb-query