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 badges1 Answer
Reset to default 0You 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
版权声明:本文标题:javascript - Find by array of Ids not working with Mongoose aggregate method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741413268a2377362.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论