我尝试做的是获取一个数组内部的对象值,模型如下:
_id:630e2a2250283de03b2dc920
fullName: John
phone:"+1234..."
createdAt:2022-08-30T15:17:54.608+00:00
selectedClients:Array
0:Object
phone:"+4567..."
fullName:"Client1"
_id:630e2a8f8367a2aaac3343b4
createdAt:2022-08-30T15:19:43.372+00:00
1:Object
phone:"+7890..."
fullName:"Client2"
_id:630e2b73d42ddc8d622e860f
createdAt:2022-08-30T15:23:31.883+00:00
__v:0
因此,我想要查找的是Client1。我获得了用户ID和客户端ID。我尝试了一种方法,但它返回了整个用户模型,而不仅仅是Client1对象。
exports.removeTrustee = asyncHandler(async (req, res, next) => {
const user_id = req.params.user_id.split("-")[1];
const client_id = ObjectId(req.params.client_id.split("-")[1]);
const client = await User.find({
selectedClients: { $elemMatch: { _id: client_id } },
});
因此,基本上我想要的是只返回我作为参数放置的client_id对象。
示例:
phone:"+4567..."
fullName:"Client1"
_id:630e2a8f8367a2aaac3343b4
createdAt:2022-08-30T15:19:43.372+00:00
我该怎么做呢?
1条答案
按热度按时间d6kp6zgx1#
可以使用$elemMatch投影运算符将s这样的数组字段限制为单个匹配元素,但不能使用find删除文档的s级别: