查询 documentdb 中的子字段

Querying a subfield in documentdb

例如,我在下面有一个文件用于收集 = 交付:

{
    "doc": [
        {
            "docid": "15",
            "deliverynum": "123",
            "text": "txxxxxx",
            "date": "2019-07-18T12:37:58Z"
        },
        {
            "docid": "16",
            "deliverynum": "456",
            "text": "txxxxxx",
            "date": "2019-07-18T12:37:58Z"
        },
        {
            "docid": "17",
            "deliverynum": "999",
            "text": "txxxxxx",
            "date": "2019-07-18T12:37:58Z"
        }
    ],
    "id": "123",
    "cancelled": false
}

是否可以使用 "deliverynum" = 999 进行搜索并且输出如下所示?

{
    "doc": [        
        {
            "docid": "17",
            "deliverynum": "999",
            "text": "txxxxxx",
            "date": "2019-07-18T12:37:58Z"
        }
    ],
    "id": "123",
    "cancelled": false
}

还是我应该为 Doc 部分制作另一个 Collection?

对于这种情况,我在 C# 中进行查询时遇到问题。

在 Mongo shell 中您可以使用 $(projection) 运算符:

db.collection.find({ "doc.deliverynum": "999" }, { "doc.$": 1 })

对应的 C# 代码如下所示:

var q = Builders<Model>.Filter.ElemMatch(x => x.doc, d => d.deliverynum == "999");
var p = Builders<Model>.Projection.ElemMatch(x => x.doc, d => d.deliverynum == "999");

var data = Col.Find(q).Project(p).ToList();

如果要获取所有文档,即使不包含 deliverynum =``999

,也可以使用 q = Builders<Model>.Filter.Empty