如何使用mongoose通过数组属性查找

时间:2014-06-11 16:45:08

标签: node.js mongodb mongoose

Products:[
  {
    name: "PSP"
    tags: ["psp","sony","videogames"]
    price: 50
  },
  {
    name: "XBOX ONE"
    tags: ["xbox","one","videogames"]
    price: 50
  },
  {
    name: "Nintendo Wii"
    tags: ["wii","nintendo","videogames"]
    price: 50
  }
]

我试图按标签过滤此列表,我希望得到数组

mongoose.model('products').find({'Products.tags':'PSP'});

我想要的是这个

 {
    name: "PSP"
    tags: ["psp","sony","videogames"]
    price: 50
  }

它不起作用!

1 个答案:

答案 0 :(得分:0)

MongoDB对字符串进行区分大小写的比较。您正在尝试查找大写PSP的字符串,但您的文档包含小写字符串psp

你可以这样试试:

mongoose.model('products').findOne({'Products.tags':'psp'}).exec(
    function(err, docs) {
        if (err) return console.error(err);
        console.log(docs.tags);
});