Mongoose - How to find by an 'array' of ObjectId?
up vote
0
down vote
favorite
I'm getting an array of ObjectId
from a query, then I need to use them inside another query.
Here is the function:
exports.getAllByShop = wrap(async(req, res, next) => {
const products = await Product.find({ _shop: mongoose.Types.ObjectId(req.params.shopId) }).select('_id');
// how to make this query?
const sales = await Sale.find({ _product: { $in: [ products ]}});
res.status(200).json(sales);
});
The result of the first query products
looks like this:
[
{ _id: 5be3601f90e40b35547ae6b4 },
{ _id: 5be4b5a3443abf196cb4cc9a }
]
What I'm trying to achieve:
const sales = await Sale.find({ _product: {
$in:
[
mongoose.Types.ObjectId(prod1),
mongoose.Types.ObjectId(prod2),
...
]
}});
I need to find all the sales
that have their _product
equals to the values of the above array. Right now it doesn't work and show this message: "Cast to ObjectId failed for value "[ { _id: 5be3601f90e40b35547ae6b4 },↵ { _id: 5be4b5a3443abf196cb4cc9a } ]" at path "_product" for model "sale""
. How to make this last query work?
node.js database mongodb express mongoose
add a comment |
up vote
0
down vote
favorite
I'm getting an array of ObjectId
from a query, then I need to use them inside another query.
Here is the function:
exports.getAllByShop = wrap(async(req, res, next) => {
const products = await Product.find({ _shop: mongoose.Types.ObjectId(req.params.shopId) }).select('_id');
// how to make this query?
const sales = await Sale.find({ _product: { $in: [ products ]}});
res.status(200).json(sales);
});
The result of the first query products
looks like this:
[
{ _id: 5be3601f90e40b35547ae6b4 },
{ _id: 5be4b5a3443abf196cb4cc9a }
]
What I'm trying to achieve:
const sales = await Sale.find({ _product: {
$in:
[
mongoose.Types.ObjectId(prod1),
mongoose.Types.ObjectId(prod2),
...
]
}});
I need to find all the sales
that have their _product
equals to the values of the above array. Right now it doesn't work and show this message: "Cast to ObjectId failed for value "[ { _id: 5be3601f90e40b35547ae6b4 },↵ { _id: 5be4b5a3443abf196cb4cc9a } ]" at path "_product" for model "sale""
. How to make this last query work?
node.js database mongodb express mongoose
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm getting an array of ObjectId
from a query, then I need to use them inside another query.
Here is the function:
exports.getAllByShop = wrap(async(req, res, next) => {
const products = await Product.find({ _shop: mongoose.Types.ObjectId(req.params.shopId) }).select('_id');
// how to make this query?
const sales = await Sale.find({ _product: { $in: [ products ]}});
res.status(200).json(sales);
});
The result of the first query products
looks like this:
[
{ _id: 5be3601f90e40b35547ae6b4 },
{ _id: 5be4b5a3443abf196cb4cc9a }
]
What I'm trying to achieve:
const sales = await Sale.find({ _product: {
$in:
[
mongoose.Types.ObjectId(prod1),
mongoose.Types.ObjectId(prod2),
...
]
}});
I need to find all the sales
that have their _product
equals to the values of the above array. Right now it doesn't work and show this message: "Cast to ObjectId failed for value "[ { _id: 5be3601f90e40b35547ae6b4 },↵ { _id: 5be4b5a3443abf196cb4cc9a } ]" at path "_product" for model "sale""
. How to make this last query work?
node.js database mongodb express mongoose
I'm getting an array of ObjectId
from a query, then I need to use them inside another query.
Here is the function:
exports.getAllByShop = wrap(async(req, res, next) => {
const products = await Product.find({ _shop: mongoose.Types.ObjectId(req.params.shopId) }).select('_id');
// how to make this query?
const sales = await Sale.find({ _product: { $in: [ products ]}});
res.status(200).json(sales);
});
The result of the first query products
looks like this:
[
{ _id: 5be3601f90e40b35547ae6b4 },
{ _id: 5be4b5a3443abf196cb4cc9a }
]
What I'm trying to achieve:
const sales = await Sale.find({ _product: {
$in:
[
mongoose.Types.ObjectId(prod1),
mongoose.Types.ObjectId(prod2),
...
]
}});
I need to find all the sales
that have their _product
equals to the values of the above array. Right now it doesn't work and show this message: "Cast to ObjectId failed for value "[ { _id: 5be3601f90e40b35547ae6b4 },↵ { _id: 5be4b5a3443abf196cb4cc9a } ]" at path "_product" for model "sale""
. How to make this last query work?
node.js database mongodb express mongoose
node.js database mongodb express mongoose
edited Nov 8 at 23:02
asked Nov 8 at 22:30
James
23611
23611
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
youre inserting an array, into an array, try this instead
const productIds = products.map((v) => v)
const sales = await Sale.find({ _product: { $in: productIds }});
Still the same result for the array. How to transform[ { _id: xxx }, { _id: yyy } ]
into this[ mongoose.Types.ObjectId(xxx), mongoose.Types.ObjectId(yyy) ]
?
– James
Nov 8 at 23:00
mongoose should know the type based on the schema so you should not need to cast them. but if you wanted to try just add them to the map likeproducts.map((v) => mongoose.Types.ObjectId(v))
– d3l33t
Nov 8 at 23:07
You're right. Got it working by taking off the extra[ ]
– James
Nov 8 at 23:11
great! good luck
– d3l33t
Nov 8 at 23:12
1
was going to mention populate originally, Yes you can usepopulate('_product')
and mongo will return those documents as well. populate() takes the field name not the model name
– d3l33t
Nov 8 at 23:27
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
youre inserting an array, into an array, try this instead
const productIds = products.map((v) => v)
const sales = await Sale.find({ _product: { $in: productIds }});
Still the same result for the array. How to transform[ { _id: xxx }, { _id: yyy } ]
into this[ mongoose.Types.ObjectId(xxx), mongoose.Types.ObjectId(yyy) ]
?
– James
Nov 8 at 23:00
mongoose should know the type based on the schema so you should not need to cast them. but if you wanted to try just add them to the map likeproducts.map((v) => mongoose.Types.ObjectId(v))
– d3l33t
Nov 8 at 23:07
You're right. Got it working by taking off the extra[ ]
– James
Nov 8 at 23:11
great! good luck
– d3l33t
Nov 8 at 23:12
1
was going to mention populate originally, Yes you can usepopulate('_product')
and mongo will return those documents as well. populate() takes the field name not the model name
– d3l33t
Nov 8 at 23:27
|
show 1 more comment
up vote
1
down vote
accepted
youre inserting an array, into an array, try this instead
const productIds = products.map((v) => v)
const sales = await Sale.find({ _product: { $in: productIds }});
Still the same result for the array. How to transform[ { _id: xxx }, { _id: yyy } ]
into this[ mongoose.Types.ObjectId(xxx), mongoose.Types.ObjectId(yyy) ]
?
– James
Nov 8 at 23:00
mongoose should know the type based on the schema so you should not need to cast them. but if you wanted to try just add them to the map likeproducts.map((v) => mongoose.Types.ObjectId(v))
– d3l33t
Nov 8 at 23:07
You're right. Got it working by taking off the extra[ ]
– James
Nov 8 at 23:11
great! good luck
– d3l33t
Nov 8 at 23:12
1
was going to mention populate originally, Yes you can usepopulate('_product')
and mongo will return those documents as well. populate() takes the field name not the model name
– d3l33t
Nov 8 at 23:27
|
show 1 more comment
up vote
1
down vote
accepted
up vote
1
down vote
accepted
youre inserting an array, into an array, try this instead
const productIds = products.map((v) => v)
const sales = await Sale.find({ _product: { $in: productIds }});
youre inserting an array, into an array, try this instead
const productIds = products.map((v) => v)
const sales = await Sale.find({ _product: { $in: productIds }});
answered Nov 8 at 22:52
d3l33t
64549
64549
Still the same result for the array. How to transform[ { _id: xxx }, { _id: yyy } ]
into this[ mongoose.Types.ObjectId(xxx), mongoose.Types.ObjectId(yyy) ]
?
– James
Nov 8 at 23:00
mongoose should know the type based on the schema so you should not need to cast them. but if you wanted to try just add them to the map likeproducts.map((v) => mongoose.Types.ObjectId(v))
– d3l33t
Nov 8 at 23:07
You're right. Got it working by taking off the extra[ ]
– James
Nov 8 at 23:11
great! good luck
– d3l33t
Nov 8 at 23:12
1
was going to mention populate originally, Yes you can usepopulate('_product')
and mongo will return those documents as well. populate() takes the field name not the model name
– d3l33t
Nov 8 at 23:27
|
show 1 more comment
Still the same result for the array. How to transform[ { _id: xxx }, { _id: yyy } ]
into this[ mongoose.Types.ObjectId(xxx), mongoose.Types.ObjectId(yyy) ]
?
– James
Nov 8 at 23:00
mongoose should know the type based on the schema so you should not need to cast them. but if you wanted to try just add them to the map likeproducts.map((v) => mongoose.Types.ObjectId(v))
– d3l33t
Nov 8 at 23:07
You're right. Got it working by taking off the extra[ ]
– James
Nov 8 at 23:11
great! good luck
– d3l33t
Nov 8 at 23:12
1
was going to mention populate originally, Yes you can usepopulate('_product')
and mongo will return those documents as well. populate() takes the field name not the model name
– d3l33t
Nov 8 at 23:27
Still the same result for the array. How to transform
[ { _id: xxx }, { _id: yyy } ]
into this [ mongoose.Types.ObjectId(xxx), mongoose.Types.ObjectId(yyy) ]
?– James
Nov 8 at 23:00
Still the same result for the array. How to transform
[ { _id: xxx }, { _id: yyy } ]
into this [ mongoose.Types.ObjectId(xxx), mongoose.Types.ObjectId(yyy) ]
?– James
Nov 8 at 23:00
mongoose should know the type based on the schema so you should not need to cast them. but if you wanted to try just add them to the map like
products.map((v) => mongoose.Types.ObjectId(v))
– d3l33t
Nov 8 at 23:07
mongoose should know the type based on the schema so you should not need to cast them. but if you wanted to try just add them to the map like
products.map((v) => mongoose.Types.ObjectId(v))
– d3l33t
Nov 8 at 23:07
You're right. Got it working by taking off the extra
[ ]
– James
Nov 8 at 23:11
You're right. Got it working by taking off the extra
[ ]
– James
Nov 8 at 23:11
great! good luck
– d3l33t
Nov 8 at 23:12
great! good luck
– d3l33t
Nov 8 at 23:12
1
1
was going to mention populate originally, Yes you can use
populate('_product')
and mongo will return those documents as well. populate() takes the field name not the model name– d3l33t
Nov 8 at 23:27
was going to mention populate originally, Yes you can use
populate('_product')
and mongo will return those documents as well. populate() takes the field name not the model name– d3l33t
Nov 8 at 23:27
|
show 1 more comment
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217130%2fmongoose-how-to-find-by-an-array-of-objectid%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown