仅使用$ elemMatch返回子数组的第一个元素

时间:2022-05-28 03:36:27

I'm using node and mongoose, and have a schema that looks like this:

我正在使用node和mongoose,并且有一个如下所示的模式:

var SubscriberSchema = new Schema({
    'user': [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
    'level': { type: String, enum: [ 'owner', 'sub', 'commenter', 'poster' ] }
    'dateAdded': { type: Date, default: Date.now }
});

// Group Schema
var GroupSchema = new Schema({
    'groupOwner': [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
    'groupName': String,
    'subscribers': [SubscriberSchema],
});

I would like to query the group to find all groups where a user (stored in req.user._id via token authentication) is a subscriber (i.e. their _id is in the subscribers array), and only return the single subscribers array element with their _id.

我想查询该组以查找用户(通过令牌认证存储在req.user._id中)是订阅者的所有组(即他们的_id在订阅者数组中),并且仅返回单个订阅者数组元素_ID。

I've read the Mongo documentation on $elemMatch as this seems to be what I need, and built the query below. This returns the information I want, but returns all elements of the subscribers array. How can I return only the single element of the subscribers array that matches my req.user._id?

我已经阅读了关于$ elemMatch的Mongo文档,因为这似乎是我需要的,并构建了下面的查询。这将返回我想要的信息,但返回订阅者数组的所有元素。如何只返回与我的req.user._id匹配的subscriber数组的单个元素?

Current query, returns all elements of subscribers:

当前查询,返回订阅者的所有元素:

            Group
                .find( { "subscribers.user": req.user._id}, { subscribers: { $elemMatch: { user: req.user._id }}} )
                .sort('groupName')
                .populate('groupOwner', 'email firstName lastName')
                .populate('subscribers.user', 'email firstName lastName')
                .exec(function(err, data) {
                    if (err) {
                        logger.error('Unable to retrieve groups for user: ' + err.message);
                        res.status(500)
                    } else {
                        res.json(data);
                    }
                });

This returns the following for subscribers (via util.inspect(data[0].subscribers)):

这将为订阅者返回以下内容(通过util.inspect(data [0] .subscribers)):

Subscribers
 [{ 
  user: 
   [ { _id: 1234,
       email: 'me@here.com',
       firstName: 'Testy',
       lastName: 'Testelson' } ] }
  user: 
   [ { _id: 5678,
       email: 'you@there.com',
       firstName: 'Biggy',
       lastName: 'Smalls' } ] }]

Based on the $elemMatch docs, I would assume I would only see user 1234 since that's the record that matches req.user._id. What am I doing wrong here?

基于$ elemMatch文档,我假设我只会看到用户1234,因为那是与req.user._id匹配的记录。我在这做错了什么?

Thanks!

1 个解决方案

#1


2  

In your projection parameter, use the dollar operator:

在您的投影参数中,使用美元运算符:

{"user.$": 1}

This will return a Group with only a single object in its 'subscribers' array.

这将返回一个Group,其“subscriber”数组中只有一个对象。

#1


2  

In your projection parameter, use the dollar operator:

在您的投影参数中,使用美元运算符:

{"user.$": 1}

This will return a Group with only a single object in its 'subscribers' array.

这将返回一个Group,其“subscriber”数组中只有一个对象。