doubinduo3364 2016-06-09 09: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 10: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");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!