【问题标题】:Does one array contain any value in the other array Mongodb using c#一个数组是否包含使用 c# 的另一个数组 Mongodb 中的任何值
【发布时间】:2021-12-08 21:11:16
【问题描述】:
{
  "objects": [
    {
      "id": 123,
      "tracking_datas": [
        {
          "id": 1,
          "polygons": [1,3]
        },
        {
          "id": 2,
          "polygons": [3]
        },
        {
          "id": 3,
          "polygons": [1,2]
        }
      ]
    }
  ]
}

我有一个如上所述的 json 文件。而且在我的NetCore项目中有一个满足这个json的模型。我想获取包含在 mongodb 的帮助下确定的多边形 ID 的对象。如何使用 c# mongo db 做到这一点?

例如,我有一个引用数组 requiredPolygons: [1,2] ,我想在 json 中的对象的跟踪数据中获取包含这些多边形 ID 的数据。预期结果如下。

{
  "objects":
  [
    {
      "id": 123,
      "tracking_datas":[
        {
          "id": 1,
          "polygons": [1,3]
        },
        {
          "id": 3,
          "polygons": [1,2]
        }
        ]
    }
  ]
}

【问题讨论】:

    标签: c# arrays .net mongodb asp.net-core


    【解决方案1】:
        public class Test
        {
            public ObjectId Id { get; set; }
            public IEnumerable<Object> objects { get; set; }
            [BsonExtraElements]
            public BsonDocument UnmappedFields { get; set; } // I'm not sure why it's required, something wrong with mapping configuration,
                                                             // but it's a separate question
        }
    
        public class Object
        {
            public int id { get; set; }
            public IEnumerable<TrackingData> tracking_datas { get; set; }
        }
    
        public class TrackingData
        {
            public int id { get; set; }
            public IEnumerable<int> polygons { get; set; }
            [BsonExtraElements]
            public BsonDocument UnmappedFields { get; set; } // I'm not sure why it's required, something wrong with mapping configuration,
                                                             // but it's a separate question
        }
    
         var json = @"{
      ""objects"": [
        {
                    ""id"": 123,
          ""tracking_datas"": [
            {
                        ""id"": 1,
              ""polygons"": [1,3]
            },
            {
                        ""id"": 2,
              ""polygons"": [3]
            },
            {
                        ""id"": 3,
              ""polygons"": [1,2]
            }
          ]
        }
      ]
    }";
                var client = new MongoClient();
                var db = client.GetDatabase("so_test");
                var coll = db.GetCollection<BsonDocument>("coll");
    
                coll.InsertOne(BsonDocument.Parse(json));
    
                var ids = new[] { 1, 2 };
    
                var typedColl = db.GetCollection<Test>("coll");
                var result = typedColl
                    .Aggregate()
                    .Project(p =>
                        new Test
                        {
                            Id = p.Id,
                            objects = p.objects.Select(o =>
                                new Object
                                {
                                    id = o.id,
                                    tracking_datas = o.tracking_datas.Where(t => t.polygons.Any(p=>ids.Contains(p)))
                                })
                        }
                    )
                    .ToList();
    

    【讨论】:

      【解决方案2】:

      给你:

      db.collection.find({
        "objects.tracking_datas.polygons": {
          $in: [
            1,
            2
          ]
        }
      })
      

      https://mongoplayground.net/p/MDlIV3YPkZB

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-16
        • 2023-03-08
        • 2011-07-19
        • 2022-01-09
        相关资源
        最近更新 更多