doucong8553 2013-07-31 09:04
浏览 282
已采纳

如何通过mongodb中的Tag有条件地查找项目

I am new to NoSQL and MongoDB and I am a little puzzled on what type of queries I can do and how to do them. my knowledge is limited to simpler queries

I would like to make what I think its a complicated query within MongoDB instead of using PHP to sort it but I do not know if it is possible or how to do it.

I have a tag field within my collection that is an array. {tag: ["blue","red","yellow","green","violet"]}.

First level problem: Let says I want to find all birds that have the tag blue & yellow & green, where blue is a must have tag and any other colours are optional.

Second level problem: Then I would like to order the query so that the birds that have all the queried colours appear first.

Is it possible to create this query in mongoDB? and if it is How could I do it?

enter image description here

  • 写回答

2条回答 默认 最新

  • douwu0335 2013-07-31 09:32
    关注

    Most of this you will have to do in your application. In order to find all documents where a bird has the tag "blue", you can do this:

    db.collection.find( { tag: "blue" } );
    

    Which colours are optional doesn't matter, as you have to find by the required tag anyway.

    After finding them, you need to do a sort. But sorting like you want (by their 3 colours) is not something you can do in MongoDB, and something you will have to do in PHP instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统