doubinduo3364
2016-06-09 09:52
浏览 88

在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!

图片转代码服务由CSDN问答提供 功能建议

我想从MongoDB数据库中提取一个ean列表。 使用“find()”我分开了 文档。 我想在文档的一个独特视图中获得一个ean列表,或者最多在一个数组中获取。 我知道我可以在php中读取数据并处理它们。 但是因为我正在学习使用MongoDB和 PHP的“MongoDB驱动程序”我想了解如何使用命令直接从数据库中提取数据。

我的数据库:

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

我想获得(或类似):

  {
“ean”:[
“4960999612638”
“4960999150437”  
“0050332160514”
“4960999213743”
“0050332143265”
“0050332143258”
 
 
} 
} 
   
 
 

我还要del 结果重复ean,但这是另一个故事...

我可以用mongodb做到这一点吗?

感谢所有想要帮助我的人 !

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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");
    
    点赞 评论

相关推荐 更多相似问题