gpt4 book ai didi

c# - 更新 MongoDB 集合中的条目

转载 作者:可可西里 更新时间:2023-11-01 09:44:23 24 4
gpt4 key购买 nike

我不知道如何更新我的 MongoDB 集合中的条目。我看过 C# 驱动程序 docs ,我想我已经非常密切地关注了他们。

但是,我传递给 Update 方法的参数中的一个(或者两个?)无效。谁能告诉我我做错了什么?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using MongoDB.Driver.Linq;


namespace Csharp_Linq
{
class Program
{
public class Book
{
// Fields
public string title { get; set; }
public string author { get; set; }
public ObjectId id { get; set; }

// Constructors
public Book()
{
this.title = "some title";
this.author = "some author";
}

public Book(string title, string author)
{
this.title = title;
this.author = author;
}

}

static void Main(string[] args)
{
// Connect to the server
string connectionString = "mongodb://localhost";
MongoClient client = new MongoClient(connectionString);
MongoServer server = client.GetServer();

// Get the database then the collection
MongoDatabase database = server.GetDatabase("tutorial");
MongoCollection collection = database.GetCollection("books");

// Query the collection
int count =
(from book in collection.AsQueryable<Book>()
select book)
.Count();

string numBooks = String.Format("This collection has {0} books.", count);
Console.WriteLine(numBooks);

var query =
from book in collection.AsQueryable<Book>()
where book.author == "Ernest Hemingway"
select book;

foreach (var book in query)
{
string bookInfo = String.Format("{0} by {1}", book.title, book.author);
Console.WriteLine(bookInfo);
}

// Insert new books
Book scaryBook = new Book("Dr. Sleep", "Stephen King");
Book[] batch =
{
new Book(),
scaryBook
};
collection.InsertBatch(batch);

// Update default book
var query2 =
from book in collection.AsQueryable<Book>()
where book.title == "some title" && book.author == "some author"
select book;

var update = new UpdateDocument {
{ "$set", new BsonDocument("title", "War and Peace") }
};
BsonDocument updatedBook = collection.Update(query2, update);

Console.ReadLine();
}
}
}

我有点惊讶 Update 方法实际上返回一个 BsonDocument。为什么要这样做?

早些时候我尝试使用 Update 对象,如示例所示:

MongoCollection<BsonDocument> books;
var query = Query.And(
Query.EQ("author", "Kurt Vonnegut"),
Query.EQ("title", "Cats Craddle")
);
var update = Update.Set("title", "Cat's Cradle");
BsonDocument updatedBook = books.Update(query, update);

这个对象还存在吗?每当我将它键入 Visual Studio 时,我都会收到一条错误消息,指出该对象不在命名空间中。

最佳答案

首先我必须包括

using MongoDB.Driver.Builders;

然后我不得不将我的 Linq 查询转换为 Mongo 查询。这是结果代码:

        // Update default book
var query2 =
from book in collection.AsQueryable<Book>()
where book.title == "some title" && book.author == "some author"
select book;
// Cast linq query to Mongo query
var mongoQuery = ((MongoQueryable<Book>)query2).GetMongoQuery();

var update = new UpdateDocument {
{ "$set", new BsonDocument("title", "War and Peace") }
};
collection.Update(mongoQuery, update);

进行了一些研究以使所有部件都适合!

关于c# - 更新 MongoDB 集合中的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24293256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com