Return documents where all entries matches in Documents array field

5 views
Skip to first unread message

Niklas Fondberg

unread,
Sep 19, 2015, 6:28:24 AM9/19/15
to Mongoose Node.JS ODM
I am giving up searching and reading for a way to return documents where all entries exists in an array.
It seems $in returns matches based on "any" and $all returns what I want but only the field (not the whole document)
The result I want is all documents that have all tags provided in the search.

The model looks like this:
var AssetSchema   = new Schema({
name: String,
url: String,
tags: [String]
});

A simple DB looks like this:
[
  {
    _id: "55fd2974a3fce70d58c23bd0",
    name: "Lord of the rings",
    __v: 0,
    tags: [
      "channel:svt",
      "user:12345"
    ]
  },
  {
    _id: "55fd29b9a3fce70d58c23bd1",
    name: "Always Ultra",
    url: "http://x/x.m3u8",
    __v: 0,
    tags: [
      "channel:svt",
      "user:12346"
    ]
  }
]

The code looks like this:
Asset.find(({tags: {$in: ["user:123456","channel:svt"]}})
 .exec(function(err, assets) {
  console.log(assets);
});


Niklas Fondberg

unread,
Sep 19, 2015, 6:35:59 AM9/19/15
to Mongoose Node.JS ODM
Very strange. Now it seems to work with $all as operator.

Reply all
Reply to author
Forward
0 new messages