doubinduo3364 2016-06-09 01:52
浏览 103
已采纳

在MongoDb上查找多个文档值到一个文档中

I would like to extract a list of ean from my MongoDB database. With "find ()" I get separate documents. I would like to get a list of ean in a unique view of the document, or at most in an array. I know that I could read the data in php and process them. But since I'm learning to use MongoDB and "MongoDB Driver" for PHP I would like to understand how to extract data directly from the database using commands.

My DB:

{
   "_id" : "ID0001",
   "ean" : [ 
       "4960999612638", 
       "4960999150437",
       "0050332160514"
   ]
}
{
   "_id" : "ID0002",
   "ean" : [ 
       "4960999213743"
   ]
}
{
    "_id" : "ID0003",
    "ean" : [ 
        "0050332143265", 
        "0050332143258"
    ]
}
{
    "_id" : "ID0004",
    "ean" : [ 
        "0050332160514"
    ]
}

What I would like to get (or similar):

{
   "ean" : [
      "4960999612638"
      "4960999150437"
      "0050332160514"
      "4960999213743"
      "0050332143265"
      "0050332143258"
   ]
}

I would also delete duplicate ean by the results, but that's another story...

I can do this with mongodb?

Thanks to all those want help me!

展开全部

  • 写回答

1条回答 默认 最新

  • dousi5358 2016-06-09 02:03
    关注

    I would suggest using distinct function. I am not very familiar with php but I think, the code would look like that:

    $collection->distinct("ean");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部