如何从Mongo DB中的JSON结构(文档)中提取数据

时间:2015-12-18 12:36:00

标签: arrays mongodb codeigniter

我正在使用Mongo DB,我有以下文件:

{
        "_id" : ObjectId("5673f753dbafbed2f4b12afc"),
        "user_id" : "5",
        "user_name" : "Aditya",
        "thought" : [
                {
                        "thought_id" : "4",
                        "thought_text" : "aditya here",
                        "th_inserted_at" : "2015-12-17 11:50:35",
                        "comments" : [
                                {
                                        "cmt_from_id" : "4",
                                        "comment_txt" : "stop bala",
                                        "cmt_inserted_at" : "2015-12-17 12:50:35
"
                                }
                        ]
                }
        ],
        "followings" : [
                {
                        "following_id" : "4"
                },
                {
                        "following_id" : "1"
                }
        ],
        "followers" : [
                {
                        "follower_id" : "4"
                },
                {
                        "follower_id" : "2"
                },
                {
                        "follower_id" : "1"
                }
        ],
        "role" : "user",
        "profile_pic" : "somepic",
        "review" : [
                {
                        "review_id" : "2",
                        "review_txt" : "nice play",
                        "play_id" : "1",
                        "rev_inserted_at" : "2015-12-17 18:50:35"
                }
        ]
}

如何使用Codeigniter mongo DB' s where子句获取嵌套文档。例如,我想获取思想部分中think_id = 4的所有数据。 我正在使用this library

2 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

// Query MongoDB: Active document library usage example
$info = $this->mongo_db->where('thought_id', '4')->get('collection_name_here');  

// Query MongoDB: Command usage example
$info= $this->mongo_db->command(array(
    'geoNear'    => 'buildings', 
    'near'       => array(53.228482, -0.547847), 
    'num'        => 10, 
    'nearSphere' => TRUE,
));

答案 1 :(得分:0)

我在Mongo DB终端中解雇了以下查询:

  db.wall.find({},
   {thought: { $elemMatch: { thought_id: "4" }} })

我按预期得到以下结果:

{ 
    "_id" : ObjectId("5673f753dbafbed2f4b12afc"), 
    "thought" : [
        {
            "thought_id" : "4", 
            "thought_text" : "aditya here", 
            "th_inserted_at" : "2015-12-17 11:50:35", 
            "comments" : [
                {
                    "cmt_from_id" : "4", 
                    "comment_txt" : "stop bala", 
                    "cmt_inserted_at" : "2015-12-17 12:50:35"
                }
            ]
        }
    ]
}

现在如何使用此library ???

在Codeigniter语法中表示上述查询