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

为什么我的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 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形