1

I have been struggling for a while now and i can't seem to find an answer here. I want to filter an array of subschemas in a subdocument by removing the ones that exist in an already specified list (using $nin:). My problem is that the list I want to check against is on the parent of the sub document, take a look at what I want to do:

User.find({_id :  socketUser.User})
            .populate("room")
            .elemMatch("room.things", {"Id" : {$nin : "User.likedThings" }})
            .exec(function(err,users){
                console.log(err);

            });

So my user has an attribute called room in while I have a list of things. The user also has an array of unique ids (not ObjectIds, strings) of "likedThings".

What I want to do is to get the "Things" for the room which ids are not already in the users list of "likedThings".

My issue is that I can't access User.likedThings when I am "down" in room. ( there is no ../../ -like command).

How do I achieve this?

arneson
  • 141
  • 8
  • Umm. That's called a "join". MongoDB does not do "joins". You need to read the literature on the web already. And also understand how to structure your data to avoid that problem. –  Jun 02 '15 at 15:18
  • @user3561036 Yes, I am well aware that this would, in a relational db, be a join and that MongoDB doesn't do joins. However, this must still be possible, since the projection syntax exists. It doesn't seem reasonable that you only are allowed to match by values you "know" (not other attributes)? – arneson Jun 02 '15 at 15:22
  • 1
    No it is not possible without restructuring your data. Mongoose `.populate()` is not "magic". All it does is issue another query(ies) to retrieve the "related" content. This is well documented. I suggest [Data Modelling](http://docs.mongodb.org/manual/data-modeling/) in the official guide, if not further reading of the Mongoose documentation for a general introduction. –  Jun 02 '15 at 15:26

0 Answers0