0

I got a problem with finding lines in a structure made of objects and arrays.

Here is my mongoose schema :

var subscriberSchema = mongoose.Schema({
    chkpt: {
        subDocs:[
            {
                id: String,
                newValues: {
                    attributes:[
                        {
                            _id: {
                                name: String
                            },
                            value: [
                                {
                                    string: String
                                }
                            ]
                        }
                    ]
                }
            }
        ] 
    }
});

And here is the request i try to apply on that schema :

Subscriber.find({'chkpt.modifiedSubDocs.newValues.attributes.id.name':'blabla'}, 'chkpt.modifiedSubDocs.newValues.attributes.value.string', function (err, subscribers) {
        if (err) return handleError(err);
        // Prints the json
        res.json(subscribers);

The problem is that the returned json contains every attributes arrays that contains at least one "blabla" in its ids. I would like it to return only the value that corresponds to the line of 'attributes' array which has the id named "blabla".

If you know a method to do that, don't hesitate ;)

Thanks,

Bowbow

Bowbow
  • 1

1 Answers1

0

You need to use the $ placement operator, in your projection, try to put chkpt.subDocs.$.newValues.attributes.$.value.string

Nicolas
  • 457
  • 5
  • 15
  • Unfortunately i get an error when i had the $ placement operators. ReferenceError: handleError is not defined – Bowbow Apr 27 '18 at 10:17
  • I don't really know Mongoose, but I think you need to put braces for the projection like `Subscriber.find({'chkpt.subDocs.newValues.attributes.id.name':'blabla'}, {'chkpt.subDocs.$.newValues.attributes.$.value.string'}, function())` – Nicolas Apr 27 '18 at 11:45
  • Thanks for your comment but it doesn't work with braces :( – Bowbow Apr 27 '18 at 13:48
  • I see that you have put `modifiedSubDocs` while in your schema the field is named `subDocs`. Did you modified your query to match the right field name ? – Nicolas Apr 27 '18 at 14:15
  • Yes i renamed the field when i wrote the topic, but i forgot to rename it in the query... – Bowbow Apr 27 '18 at 15:07
  • In fact, what i really want my request to do is : finding all the chkpt/subDocs/newValues/attributes/value/string that have chkpt/subDocs/newValues/attributes/ = 'blabla' – Bowbow Apr 27 '18 at 15:09