Node.js Mongoose-根据布尔属性对查找进行排序
我有一个mongoose find请求,我有第一个mongoose模式,其中包含以下内容(客户端): 我有客户端模式:Node.js Mongoose-根据布尔属性对查找进行排序,node.js,express,mongoose,Node.js,Express,Mongoose,我有一个mongoose find请求,我有第一个mongoose模式,其中包含以下内容(客户端): 我有客户端模式: new mongoose.Schema({ firstName: String, lastName: String, company: String, companyAsPrimaryName: Boolean, }) 我想在第一个模式中执行一个查找请求,该请求取决于客户机属性companyAsPrimaryName boolean。 如果为false:我想按
new mongoose.Schema({
firstName: String,
lastName: String,
company: String,
companyAsPrimaryName: Boolean,
})
我想在第一个模式中执行一个查找请求,该请求取决于客户机属性companyAsPrimaryName boolean。
如果为false:我想按firstName+lastName排序,如果为true,我想按company排序
我不知道该怎么做,我已经检查了答案,但什么也没找到
感谢您的帮助您可以使用
聚合
和$addFields
和$cond
创建一个具有您要排序依据的名称的字段,然后按该字段排序。如果companyAsPrimaryName
为false,下面的代码片段将获取每个Foo
,并根据公司名称或名字+姓氏对它们进行排序
const data = await Foo.aggregate([
{
// basically .populate('client') in our case
$lookup: {
from: 'clients',
localField: 'client',
foreignField: '_id',
as: 'client'
}
},
{
// deconstruct the client array into one object
$unwind: '$client'
},
{
// add a computed field for sorting
$addFields: {
sortName: {
$cond: {
if: {
$eq: ['$client.companyAsPrimaryName', true]
},
then: '$client.company',
else: {
$concat: ['$client.firstName', '$client.lastName']
}
}
}
}
},
{
$sort: {
sortName: 1
}
}
])
console.log(data)
这里的Foo
是包含客户机作为字段的模型。请注意,这将包括结果中的该字段。如果要省略某些字段,可以添加一个阶段来拾取字段
下面是我用来在本地测试的一个片段:
const mongoose=require('mongoose'))
const Client=mongoose.model('Client'{
名字:String,
姓氏:String,
公司名称:String,
companyAsPrimaryName:布尔值
})
const Foo=mongoose.model('Foo'{
名称:String,
客户端:{type:mongoose.Types.ObjectId,ref:'client'}
})
//在'start()'中调用此函数以填充一些示例数据
const create=async()=>{
const client1=等待新客户端({
名字:'阿尔法',
lastName:'Alphaname',
公司:“字母表”,
公司名称:true
}).save()
const client2=等待新客户端({
名字:“约翰”,
姓:“韦恩”,
公司:“谷歌”,
公司名称:true
}).save()
const client3=等待新客户端({
名字:“好极了”,
姓氏:“Bretty”,
公司:“祖鲁解决方案”,
公司主要名称:false
}).save()
等待新富({
名称:“烤面包机”,
客户:客户1
}).save()
等待新富({
名称:‘午餐盒’,
客户:客户1
}).save()
等待新富({
名称:'跑步机',
客户:客户1
}).save()
等待新富({
名称:'Tapas',
客户:客户2
}).save()
等待新富({
名称:'Ananas',
客户:客户2
}).save()
等待新富({
姓名:“Zapiers”,
客户:客户2
}).save()
等待新富({
名称:'Brets',
客户:客户3
}).save()
等待新富({
名称:“Xrats”,
客户:客户3
}).save()
等待新富({
姓名:“Abins”,
客户:客户3
}).save()
}
const start=async()=>{
等待mongoose.connect('mongodb+srv://CONNECTION 字符串在这里'{
useNewUrlParser:true,
useUnifiedTopology:正确
})
常量数据=等待Foo.aggregate([
{
//基本上。在我们的案例中填充(“客户”)
$lookup:{
来自:“客户”,
localField:'客户端',
foreignField:“\u id”,
作为:“客户”
}
},
{
//将客户端数组解构为一个对象
$unwind:“$client”
},
{
//添加用于排序的计算字段
$addFields:{
sortName:{
$cond:{
如果:{
$eq:['$client.companyAsPrimaryName',true]
},
然后:“$client.company”,
其他:{
$concat:['$client.firstName','$client.lastName']
}
}
}
}
},
{
$sort:{
sortName:1
}
}
])
console.log(数据)
}
开始(),然后(()=>{
console.log('ready')
mongoose.disconnect()
})
这些答案对您有帮助吗?:和
const data = await Foo.aggregate([
{
// basically .populate('client') in our case
$lookup: {
from: 'clients',
localField: 'client',
foreignField: '_id',
as: 'client'
}
},
{
// deconstruct the client array into one object
$unwind: '$client'
},
{
// add a computed field for sorting
$addFields: {
sortName: {
$cond: {
if: {
$eq: ['$client.companyAsPrimaryName', true]
},
then: '$client.company',
else: {
$concat: ['$client.firstName', '$client.lastName']
}
}
}
}
},
{
$sort: {
sortName: 1
}
}
])
console.log(data)