Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mongoose find().populate().sort()不';在版本5.9.9中无法工作_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js Mongoose find().populate().sort()不';在版本5.9.9中无法工作

Node.js Mongoose find().populate().sort()不';在版本5.9.9中无法工作,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有两种模式:order和client。我想创建一个填充客户端并按公司排序,但排序不起作用。我为什么会有这个问题?我该如何解决它?谢谢 客户 const mongoose = require('mongoose') const ClientSchema = mongoose.Schema({ name: { type: String, require: true, trim: true }, surname: { type: String,

我有两种模式:
order
client
。我想创建一个填充
客户端
并按
公司
排序,但排序不起作用。我为什么会有这个问题?我该如何解决它?谢谢

客户

const mongoose = require('mongoose')

const ClientSchema = mongoose.Schema({
  name: {
    type: String,
    require: true,
    trim: true
  },
  surname: {
    type: String,
    require: true,
    trim: true
  },
  company: {
    type: String,
    require: true,
    trim: true
  },
  email: {
    type: String,
    require: true,
    trim: true,
    unique: true
  },
  phone: {
    type: String,
    trim: true
  },
  date_creation: {
    type: Date,
    default: Date.now()
  },
  salesman: {
    type: mongoose.Schema.Types.ObjectId, 
    require: true,
    ref: 'User'
  }
})
module.exports = mongoose.model('Client', ClientSchema)
命令

分解器

getOrdersBySalesman: async (_, { }, ctx) => {
      try {
        const orders = await Order.find({ salesman: ctx.user.id }).populate('client').sort({company: 1})
        return orders
      } catch (error) {
        console.log(error);
      }
    },
输出


您应该在填充阶段内进行排序,检查

您的函数可能看起来像这样

getOrdersBySalesman: async (_, { }, ctx) => {
  try {
    const orders = await Order.find({ salesman: ctx.user.id }).populate({
      path: 'client',
      options: { sort: { 'company': 1 } }
    });

    return orders;

  } catch (error) {
    console.log(error);
  }
}
希望能有帮助


更新:您可以使用聚合管道对带有填充字段的文档进行排序

像下面这样

getOrdersBySalesman: async (_, { }, ctx) => {
  try {
    const orders = await Order.aggregate([
      {
        $match: { salesman: mongoose.schema.Types.ObjectId(ctx.user.id) } // this should be of type objectId to do the match
      },
      {
        $lookup: {
          localField: "client",
          foreignField: "_id",
          from: "Client", // this is the name of the clients model
          as: "client"
        }
      },
      {
        $sort: { 'client.company': 1 }
      }
    ]);

    return orders;

  } catch (error) {
    console.log(error);
  }
}

您可以

您应该在填充阶段内进行排序,请检查

您的函数可能看起来像这样

getOrdersBySalesman: async (_, { }, ctx) => {
  try {
    const orders = await Order.find({ salesman: ctx.user.id }).populate({
      path: 'client',
      options: { sort: { 'company': 1 } }
    });

    return orders;

  } catch (error) {
    console.log(error);
  }
}
希望能有帮助


更新:您可以使用聚合管道对带有填充字段的文档进行排序

像下面这样

getOrdersBySalesman: async (_, { }, ctx) => {
  try {
    const orders = await Order.aggregate([
      {
        $match: { salesman: mongoose.schema.Types.ObjectId(ctx.user.id) } // this should be of type objectId to do the match
      },
      {
        $lookup: {
          localField: "client",
          foreignField: "_id",
          from: "Client", // this is the name of the clients model
          as: "client"
        }
      },
      {
        $sort: { 'client.company': 1 }
      }
    ]);

    return orders;

  } catch (error) {
    console.log(error);
  }
}

您可以

将排序对象设置为
{client.company';1}
。它不工作,输出与
{company:1}
相同。将排序对象设置为
{client.company';1}
。它不工作,输出与
{company 1}
相同,它不工作。。。。。像其他时候一样,它没有顺序。我已经更新了我的答案,使用聚合查找而不是mongoose填充,你能检查一下吗?我有两个问题:1=>错误:无法读取未定义的属性“类型”。我已将mongoose导入为:
const mongoose=require('mongoose')
2-我已删除{match:{}}以尝试代码,输出为[],任何我不知道为什么不起作用的结果……您是否可以尝试从$lookupwhat是数据库中客户机集合的名称的:
中的“客户机”
而不是从:
中的“客户机”
,确切的名称,如果你在compass或robo3T中检查db,这个集合的名称是什么?它不工作。。。。。像其他时候一样,它没有顺序。我已经更新了我的答案,使用聚合查找而不是mongoose填充,你能检查一下吗?我有两个问题:1=>错误:无法读取未定义的属性“类型”。我已将mongoose导入为:
const mongoose=require('mongoose')
2-我已删除{match:{}}以尝试代码,输出为[],任何我不知道为什么不起作用的结果……您是否可以尝试从$lookupwhat是数据库中客户机集合的名称的:
中的“客户机”
而不是从:
中的“客户机”
,确切名称,如果您在compass或robo3T中检查db,此集合的名称是什么?