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 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'