C#MongoDb:在字段上进行匹配匹配

问题描述 投票:0回答:1

所以我有一个对象列表,其中一些将存在于Mongo中,而某些则不会。

  • 对于存在的那些,我想更新1个字段
  • 对于不存在的对象,插入整页对象。

我想通过他们的网址找到他们。反正有这样做吗?

var webapges = new List<WriteModel<Page>>();
var filterDefinition = Builders<Page>.Filter.Eq(p => p.url, **Object.url**);
var updateDefinition = Builders<Page>.Update.Set(p => p.pop, p.pop + **Object.pop??**);
listWrites.Add(new UpdateOneModel<Page>(filterDefinition, updateDefinition));
await userCollection.BulkWriteAsync(listWrites);


public class Page
    {
        [BsonId] public ObjectId Id { get; set; }
        [BsonElement("url")] public string Url { get; set; }
        [BsonElement("level")] public int Level { get; set; }
        [BsonElement("languages")] public string Languages { get; set; }
        [BsonElement("proc")] public int Proc { get; set; }
        [BsonElement("domain")] public string Domain { get; set; }
        [BsonElement("len")] public int Len { get; set; }
        [BsonElement("html")] public string Html { get; set; }
        [BsonElement("body")] public string Body { get; set; }
        [BsonElement("title")] public string Title { get; set; }
        [BsonElement("meta")] public string Meta { get; set; }
        [BsonElement("scan_date")] public BsonDateTime ScanDate { get; set; }
        [BsonElement("pop")] public int Popularity { get; set; }
}
c# mongodb mongodb-.net-driver
1个回答
1
投票

正如乔在评论中所说,您可以使每个更新为Upset,这是UpdateOneModel<T>上的一个属性,然后您必须使用$ setOnInsert更新运算符设置要在插入内容上设置的每个属性。

因此,让我们首先建立一个包含一些数据的全新数据库:

var client = new MongoClient();
var database = client.GetDatabase("test");
await client.DropDatabaseAsync(database.DatabaseNamespace.DatabaseName);
var collection = database.GetCollection<Page>("collection1");

// Create our mix of pages
var pages = new List<Page>
{
    new Page {Url = "https://some-url/1", Body = "body1", Popularity = 0, ScanDate = DateTime.UtcNow},
    new Page {Url = "https://some-url/2", Body = "body1", Popularity = 0, ScanDate = DateTime.UtcNow},
    new Page {Url = "https://some-url/3", Body = "body1", Popularity = 0, ScanDate = DateTime.UtcNow}
};

// Insert the middle one.
await collection.InsertOneAsync(pages[1]);

Debugger.Break();

现在,如果我们进入shell并查看到目前为止的数据,那么我们将在集合中有一页要更新。

> use test
switched to db test
> show collections
collection1
> db.collection1.find().pretty()
{
        "_id" : ObjectId("5e80824b0664ae4020ee68b3"),
        "url" : "https://some-url/2",
        "level" : 0,
        "languages" : null,
        "proc" : 0,
        "domain" : null,
        "len" : 0,
        "html" : null,
        "body" : "body1",
        "title" : null,
        "meta" : null,
        "scan_date" : ISODate("2020-03-29T11:11:07.700Z"),
        "pop" : 0
}

现在让我们将页面上的所有人气属性更新为100,以查看更改。

// Update all popularity to 100
pages.ForEach(x => x.Popularity = 100);

然后我们可以使用一些LINQ来创建更新模型,并将其发送到批处理写入。

// Create all the updates as a batch
var updateOneModels = pages.Select(x =>
{
    var filterDefinition = Builders<Page>.Filter.Eq(p => p.Url, x.Url);
    var updateDefinition = Builders<Page>.Update.Set(p => p.Popularity, x.Popularity)
        .SetOnInsert(p => p.Level, x.Level)
        .SetOnInsert(p => p.Languages, x.Languages)
        .SetOnInsert(p => p.Proc, x.Proc)
        .SetOnInsert(p => p.Domain, x.Domain)
        .SetOnInsert(p => p.Len, x.Len)
        .SetOnInsert(p => p.Html, x.Html)
        .SetOnInsert(p => p.Body, x.Body)
        .SetOnInsert(p => p.Title, x.Title)
        .SetOnInsert(p => p.Meta, x.Meta)
        .SetOnInsert(p => p.ScanDate, x.ScanDate);

    return new UpdateOneModel<Page>(filterDefinition, updateDefinition) { IsUpsert = true };
}).ToList();

现在运行批处理

// Run the batch
await collection.BulkWriteAsync(updateOneModels);

现在,如果我们查看外壳程序中的数据,我们的中间页现在已更新,并且已插入所有其他内容

> db.collection1.find().pretty()
{
        "_id" : ObjectId("5e80824b0664ae4020ee68b3"),
        "url" : "https://some-url/2",
        "level" : 0,
        "languages" : null,
        "proc" : 0,
        "domain" : null,
        "len" : 0,
        "html" : null,
        "body" : "body1",
        "title" : null,
        "meta" : null,
        "scan_date" : ISODate("2020-03-29T11:11:07.700Z"),
        "pop" : 100
}
{
        "_id" : ObjectId("5e80825cc38a0ff23e1eb326"),
        "url" : "https://some-url/1",
        "body" : "body1",
        "domain" : null,
        "html" : null,
        "languages" : null,
        "len" : 0,
        "level" : 0,
        "meta" : null,
        "pop" : 100,
        "proc" : 0,
        "scan_date" : ISODate("2020-03-29T11:11:07.699Z"),
        "title" : null
}
{
        "_id" : ObjectId("5e80825cc38a0ff23e1eb327"),
        "url" : "https://some-url/3",
        "body" : "body1",
        "domain" : null,
        "html" : null,
        "languages" : null,
        "len" : 0,
        "level" : 0,
        "meta" : null,
        "pop" : 100,
        "proc" : 0,
        "scan_date" : ISODate("2020-03-29T11:11:07.700Z"),
        "title" : null
}
© www.soinside.com 2019 - 2024. All rights reserved.