Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

317
Views
Mongolita y agregación con $búsqueda en ObjectId vs character

Trabajando con mongolite v0.9.1 (R) y MongoDB v3.4, me gustaría unir dos colecciones, la primera, el padre que contiene un ObjectId, el segundo, los hijos que contienen el valor de cadena del ObjectId de los padres.

Esta es la sintaxis básica:

 conParent$aggregate('[ { "$lookup": { "from":"Children", "localField": "_id", "foreignField": "parent_id", "as": "children" } } ]')

$lookup parece tomar solo el nombre del campo, he intentado esto, produciendo errores sintácticos:

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

Entonces, ¿hay alguna manera de lidiar con eso?

De otra manera, traté de guardar el ObjectId de los padres en los niños en formato ObjectId sin suerte (todavía obtengo una cadena en MongoDB):

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

¿Es posible almacenar una identificación como ObjectId en mongolite?

Nota: estoy haciendo inserciones masivas, por lo que no puedo lidiar con manipulaciones de cadenas JSON.

Alguna idea ?

Editar:

Aquí hay un ejemplo de las colecciones que estoy usando:

La colección principal:

 { "_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 }

La colección Infantil:

 { "_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" }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

¿Podrías intentar:

 "foreignField": "_id"

A partir del ejemplo del sitio web de 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)

Funciona también cuando ambos están configurados en _id

 "localField": "_id", "foreignField": "_id",
over 4 years ago · Santiago Trujillo Report

0

Bueno, debo decir que eso no es posible en absoluto!

Mongilite recupera _id como carácter y no contiene ninguna implementación de ObjectId .

Asi que...

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!