我有包含tags
个数组的文档.我想在网站上提供基于标签的建议,所以我需要获得包含相同标签的文档+与1个标签不匹配的文档+与2个标签不匹配的文档,等等...
我该怎么做?
我有包含tags
个数组的文档.我想在网站上提供基于标签的建议,所以我需要获得包含相同标签的文档+与1个标签不匹配的文档+与2个标签不匹配的文档,等等...
我该怎么做?
示例集合:
db.tags.insert({"tags":["red", "tall", "cheap"]});
db.tags.insert({"tags":["blue", "tall", "expensive"]});
db.tags.insert({"tags":["blue", "little", "cheap"]});
找到所有包含"蓝色"标签的内容
db.tags.find({tags: { $elemMatch: { $eq: "blue" } }})
找到所有标记为"蓝色"且仅为蓝色的
db.tags.find({tags: "blue"})
找到所有标记为"蓝色"和"便宜"的
db.tags.find({ tags: { $all: ["cheap", "blue"] } } )
找到所有不是"蓝色"
db.tags.find({tags: { $ne: "blue" } })
找到所有的"蓝色"和"便宜",但不是"红色"和"高"
在我的mongo db中不可能.从mongodb 1.9.1开始,类似的功能应该可以使用,不过(未经测试):
db.tags.find({ $and: [ {tags: { $all: ["blue", "cheap"] } }, { tags: { $nin: ["red", "tall"] } } ] })