doudi7782 2019-04-15 20:09
浏览 121
已采纳

为什么我的mongodb查询返回0个结果?

This is my database collection: Documents
With this go code, I try to get all the users who are either involved in the story or created the story with the given id.

func main() {
    for stf.DB == nil {
    }

    collection := stf.DB.Collection("user")
    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    defer cancel()

    id, _ := primitive.ObjectIDFromHex("5cb4dd7e29d3dca573a73d4c")
    fter := bson.M{"_id": id}

    involvedFilter := bson.M{"stories_involved": fter}
    createdFilter := bson.M{"stories_created": fter}

    filter := bson.M{"$or": []bson.M{involvedFilter, createdFilter}}

    cur, err := collection.Find(ctx, filter)
    if err != nil {
        log.Fatal(err.Error())
    }

    defer cur.Close(ctx)

    for cur.Next(ctx) {
        var result bson.M
        err := cur.Decode(&result)

        if err != nil {
            log.Fatal(err.Error())
        }
        fmt.Println(result)
    }
    if err := cur.Err(); err != nil {
        log.Fatal(err.Error())
    }
}


The code doesn't output any errors, but it also doesn't output any objects...
Thanks for your help in advance!

  • 写回答

1条回答 默认 最新

  • dongyan1841 2019-04-16 02:09
    关注

    Your query translates into:

    {"$or":[
       {"stories_involved":{
               "_id": ObjectId("5cb4dd7e29d3dca573a73d4c")}}, 
       {"stories_created":{
               "_id":ObjectId("5cb4dd7e29d3dca573a73d4c")}}
    ]}
    

    Which means that it's searching for either a document with a nested document i.e:

    {stories_involved: {_id: <value>}} OR {stories_created: {_id: <value>}}.

    However, the documents in the collection contains nested document array i.e:

    {stories_involved: [{_id:<value>}]} OR {stories_created: [{_id:<value>}]}

    This is the reason your query is not returning any value (and no error because the query syntax is correct).

    There are two ways of Querying a document nested in an array using dot notation. If you know the index of the array for the document, you can just specify the position:

    id, _ := primitive.ObjectIDFromHex("5cb4dd7e29d3dca573a73d4c")
    involvedFilter := bson.M{"stories_involved.0._id": id}
    createdFilter := bson.M{"stories_created.0._id": id}
    filter := bson.M{"$or": []bson.M{involvedFilter, createdFilter}}
    cur, err := collection.Find(ctx, filter)
    

    If you do not know the index position of the document nested in the array, concatenate the name of the array field, with a dot (.) and the name of the field in the nested document:

    id, _ := primitive.ObjectIDFromHex("5cb4dd7e29d3dca573a73d4c")
    involvedFilter := bson.M{"stories_involved._id": id}
    createdFilter := bson.M{"stories_created._id": id}
    filter := bson.M{"$or": []bson.M{involvedFilter, createdFilter}}
    cur, err := collection.Find(ctx, filter)
    

    See also MongoDB: Query Documents

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路