dpfqy5976 2018-05-16 10:45 采纳率: 100%
浏览 552

在ObjectId字段的两个集合之间进行$ lookup查询

I'am working on an api in Go 1.9.2 with mongoDB 3.4 I am using mgo as a driver.

I have to do an endpoint for getting the results of the requests, after inserting the request and its results in the same database with two collections.

Having two collections:

First Collection : requests

Second Collection : results

The requests collection has the format

{ "_id":ObjectId("5afc034f53c9a77a598c8345")
  "  time ":"2018-05-16 10:08:35.024352907 +0000 UTC m=+23.407317980"
  "param_request":[name:"mike",age:"30",job:"Doctor"]
}

The results collection has the format the id_request field is the _id field for the request document (as the foreign key philosophy in SQL)

{"_id":ObjectId("5afc035b53c9a77a598c8346")
 "id_request":ObjectId("5afc034f53c9a77a598c8345")
 "name":"Mike"
 "age":"30"
 "job":"Doctor"
 "city":"Tokyo"} 

{"_id":ObjectId("5afc035b53c9a77a598c8347")
 "id_request":ObjectId("5afc034f53c9a77a598c8345")
 "name":"Mike"
 "age":"30"
 "job":"Doctor"
 "city":"London"}


 {"_id":ObjectId("5afc035b53c9a77a598c8349")
 "id_request":ObjectId("5afc034f53c9a77a598c8345")
 "name":"Mike"
 "age":"30"
 "job":"Doctor"
 "city":"Berlin"
 } 

I tried to query and I found the lookup from the documentation $lookup Documentation

the result wanted :

 {
 "name":"Mike"
 "age":"30"
 "job":"Doctor"
 "city":"Berlin"

 }

 {
 "name":"Mike"
 "age":"30"
 "job":"Doctor"
 "city":"London"

 }

 {
 "name":"Mike"
 "age":"30"
 "job":"Doctor"
 "city":"Tokyo"

}

Here is what I did:

 db.results.aggregate([

  {$lookup: {from:requests, localField: "id_request",foreignField:"_id",as:”results”}},
  {$match:
         { 
      "id_request": ObjectId("5afc034f53c9a77a598c8345") }}]);

here is the error getting :

2018-05-16T11:31:51.261+0000 E QUERY [thread1] SyntaxError: missing } after property list @(shell):1:131

Here is what I have to get with the SQL philosophy as a query:

  select results .* from results join requests on 

  (results.request_id=requests._id 

               and 

    request_id='ObjectId("5afc034f53c9a77a598c8345")');
  • 写回答

2条回答 默认 最新

  • dongyuedaochen8415 2018-05-16 13:17
    关注

    The query in your question has multiple issues which may cause parsing errors. The from property in the lookup stage must be a string. Also, the quote character that you used for the as property () is not a valid string delimiter.

    Try to replace the from: requests with from: "requests" and the character with a single or double quote.

    db.results.aggregate([
       { $lookup: { from: "requests", localField: "id_request", foreignField: "_id", as: "results" } },
       { $match: { "id_request": ObjectId("5afc034f53c9a77a598c8345") } } 
    ]);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类