Querying a subfield in documentdb

后端 未结 1 1057
野趣味
野趣味 2021-01-20 14:02

For example I have a document below for collection = delivery:

{
    \"doc\": [
        {
            \"docid\": \"15\",
            \"deliverynum\": \"123\"         


        
相关标签:
1条回答
  • 2021-01-20 14:18

    In Mongo shell you can use the $(projection) operator:

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

    Corresponding C# code can look like below:

    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();
    

    You can also use q = Builders<Model>.Filter.Empty if you want to get all documents even if the don't contain deliverynum =``999

    0 讨论(0)
提交回复
热议问题