dragon2025 2014-02-13 04:04
浏览 48
已采纳

如何使用php搜索mongoId数组

My Mongo collection document is as:

Array
(
    [_id] => MongoId Object
        (
            [$id] => 52ddf885f786208bf58020df
        )
    [FirstName] => Aloma
    [State] => AR
    [Title] => AVP
    [Zip] => 71953
    [campaign_id] => Array
        (
            [0] => 52fba54fce798c441400002b
            [1] => 52fba687ce798c441400002c
        )

)

campaign_id is the array of mongoid's reference to other collection.

How can I get the documents with campaign_id = 52fba54fce798c441400002b Here we have to search mongoid 52fba54fce798c441400002b in array of campaign_id.

  • 写回答

1条回答 默认 最新

  • douyingtai6662 2014-02-13 04:44
    关注

    You can use following code in mongoDB.

    db.collection.find(campaign_id:[52fba54fce798c441400002b])
    

    It will return the documents that contains [52fba54fce798c441400002b] as campaign_id value exactly.

    For example :

    { "_id" : ObjectId("52fc4b93633d0c54e662fc75"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 11 ] }
    { "_id" : ObjectId("52fc4b8d633d0c54e662fc74"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 12, 14 ] }
    { "_id" : ObjectId("52fc4b86633d0c54e662fc73"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 11, 12, 14 ] }
    { "_id" : ObjectId("52fc4b7d633d0c54e662fc72"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 11, 12 ] }
    
    > db.scores.find({campaign_id:[11]})
    { "_id" : ObjectId("52fc4b93633d0c54e662fc75"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 11 ] }
    

    Same thing, you can implement in PHP using php mongo client.

    I hope it solves your problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并