蒙古石和$lookup聚合在ObjectId与字符上的区别

3
使用mongolite v0.9.1 (R)和MongoDB v3.4,我想要连接两个集合,第一个父集合包含ObjectId,第二个子集合包含父集合ObjectId的字符串值。
以下是基本语法:
conParent$aggregate('[
    { "$lookup":
        { "from":"Children", 
          "localField": "_id",
          "foreignField": "parent_id",
          "as": "children"
        }
    }
 ]')

$lookup似乎只接受字段名称,我尝试过这个,但出现了语法错误:

           .../...
           "foreignField": "{'$oid':'parent_id'}"
           .../...

那么有没有处理这个问题的方法呢?

另外,我试图以ObjectId格式保存父对象的ObjectId到子对象中,但是没有成功(在MongoDB中仍然得到一个字符串):

   result <- transform(
                computeFunction, 
                parent_id = sprintf('{"$oid":"%s"}',parent$"_id"))
   resultCon <- conout$insert(as.data.frame(result))

在mongolite中,是否可以将Id存储为ObjectId?

注意:我正在进行批量插入,因此无法处理JSON字符串操作。

有什么想法吗?

编辑:

这是我正在使用的集合示例:

父集合:

{
    "_id" : ObjectId("586f7e8b837abeabb778d2fd"),
    "name" : "Root1",
    "date" : "2017-01-01",
    "value" : 1.0,
    "value1" : 10.0,
    "value2" : 100.0
},
{
    "_id" : ObjectId("586f7ea4837abeabb778d30a"),
    "name" : "Root1",
    "date" : "2017-01-02",
    "value" : 2.0,
    "value1" : 20.0,
    "value2" : 200.0
}

Children集合:

{
    "_id" : ObjectId("586f7edf837abeabb778d319"),
    "name" : "Item1",
    "value" : 1.1,
    "date" : "2017-01-01",
    "parent_id" : "586f7e8b837abeabb778d2fd"
}
{
    "_id" : ObjectId("586f7efa837abeabb778d324"),
    "name" : "Item2",
    "value1" : 11.111111111,
    "value2" : 12.222222222,
    "date" : "2017-01-01",
    "parent_id" : "586f7e8b837abeabb778d2fd"
}
{
    "_id" : ObjectId("586f7f15837abeabb778d328"),
    "name" : "Item1",
    "value" : 2.2,
    "date" : "2017-01-02",
    "parent_id" : "586f7ea4837abeabb778d30a"
}
{
    "_id" : ObjectId("586f7f2b837abeabb778d32e"),
    "name" : "Item2",
    "value1" : 21.111111111,
    "value2" : 22.222222222,
    "date" : "2017-01-02",
    "parent_id" : "586f7ea4837abeabb778d30a"
}
2个回答

0

我必须说这根本不可能!

Mongilite检索_id作为字符,并不包含任何ObjectId实现。

所以...


0

你能试试:

"foreignField": "_id" 

从Mongo的官网示例开始:
library(mongolite)
library(jsonlite)

a = '[{ "_id" : 1, "item" : 1, "price" : 12, "quantity" : 2 },
{ "_id" : 2, "item" : 2, "price" : 20, "quantity" : 1 },
{ "_id" : 3  }]'

b= '[{ "_id" : 1, "sku" : "abc", "description": "product 1", "instock" : 120 },
{ "_id" : 2, "sku" : "def", "description": "product 2", "instock" : 80 },
{ "_id" : 3, "sku" : "ijk", "description": "product 3", "instock" : 60 },
{ "_id" : 4, "sku" : "jkl", "description": "product 4", "instock" : 70 },
{ "_id" : 5, "sku": null, "description": "Incomplete" },
{ "_id" : 6 }]'

mongo_orders <- mongo(db = "mydb", collection = "orders")
mongo_orders$insert(fromJSON(a))

mongo_inventory <- mongo(db = "mydb", collection = "inventory")
mongo_inventory$insert(fromJSON(b))

df <- mongo_orders$aggregate('[
    {
      "$lookup":
        {
          "from": "inventory",
          "localField": "item",
          "foreignField": "_id",
          "as": "inventory_docs"
        }
   }
]')

str(df)

当两者都设置为_id时,它同样有效。

"localField": "_id",
      "foreignField": "_id",

这个Mongo的网站示例太简单了。在现实生活中,集合的ID是不同的。而在我的情况下,父ID是通过子项的parent_id字段引用的。 - jeanjerome
你能给一个你文档结构的例子吗? - tokiloutok

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接