admin管理员组

文章数量:1289911

I would like to use the aggregate in mongoose to get documents that match the ids in the array. The array contains mongoose object ids. It is returning an empty array. Here is my code:

let isSelectedTrips = ["******", "*****"]// this actually contains trip ids.

 const me = await Trips.aggregate([
        {
          $match: { isPublished: true, _id: { $in: isSelectedTrips } },
        },
        {
          $facet: {
            getTrips: [{ $skip: skip }, { $limit: Number(searchlimit) }],
            totalDocumentSize: [{ $count: "count" }],
          },
        },
      ]);
      trips = me[0].getTrips;
      totalDocumentSize = me[0].totalDocumentSize[0]?.count || 0;

When I rewrote the conde using find method and used the same $in method, it worked. That means that I may not be doing it properly with the aggregate method. I need to use the aggregate method to also get the total document size before applying limit. I am doing this in Node.js. Here is the version that worked:

  const me = await Trips.find({ _id: { $in: isSelectedTrips }, isPublished: true, });
      trips = me;
      totalDocumentSize = 3;

How do I get the aggregate method to use array of ids to find matching documents?

I would like to use the aggregate in mongoose to get documents that match the ids in the array. The array contains mongoose object ids. It is returning an empty array. Here is my code:

let isSelectedTrips = ["******", "*****"]// this actually contains trip ids.

 const me = await Trips.aggregate([
        {
          $match: { isPublished: true, _id: { $in: isSelectedTrips } },
        },
        {
          $facet: {
            getTrips: [{ $skip: skip }, { $limit: Number(searchlimit) }],
            totalDocumentSize: [{ $count: "count" }],
          },
        },
      ]);
      trips = me[0].getTrips;
      totalDocumentSize = me[0].totalDocumentSize[0]?.count || 0;

When I rewrote the conde using find method and used the same $in method, it worked. That means that I may not be doing it properly with the aggregate method. I need to use the aggregate method to also get the total document size before applying limit. I am doing this in Node.js. Here is the version that worked:

  const me = await Trips.find({ _id: { $in: isSelectedTrips }, isPublished: true, });
      trips = me;
      totalDocumentSize = 3;

How do I get the aggregate method to use array of ids to find matching documents?

Share Improve this question edited Feb 20 at 18:49 superkingz asked Feb 20 at 18:40 superkingzsuperkingz 214 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You may want to wrap the isSelectedTrips in a mongoose ObjectId instance

let isSelectedTrips = ["******", "*****"]// this actually contains trip ids.

const objectIds = isSelectedTrips.map(id => mongoose.Types.ObjectId(id));

 const me = await Trips.aggregate([
        {
          $match: { isPublished: true, _id: { $in: objectIds } },
        },
        {
          $facet: {
            getTrips: [{ $skip: skip }, { $limit: Number(searchlimit) }],
            totalDocumentSize: [{ $count: "count" }],
          },
        },
      ]);
      trips = me[0].getTrips;
      totalDocumentSize = me[0].totalDocumentSize[0]?.count || 0;

本文标签: javascriptFind by array of Ids not working with Mongoose aggregate methodStack Overflow