简体   繁体   English

使用c#驱动程序从mongodb集合中选择所有_id

[英]Select all _id from mongodb collection with c# driver

I have large document collection in mongodb and want to get only _id list. 我在mongodb中有大型文档集,并且只想获得_id列表。 Mongodb query is db.getCollection('Documents').find({},{_id : 0, _id: 1}) . Mongodb查询是db.getCollection('Documents').find({},{_id : 0, _id: 1}) But in C# query 但是在C#查询中

IMongoCollection<T> Collection { get; set; }

...

List<BsonDocument> mongoResult = this.Collection.FindAsync(FilterDefinition<T>.Empty, new FindOptions<T, BsonDocument>() { Projection = "{ _id: 0, _id: 1 }" }).Result.ToList();

throw exeption InvalidOperationException: Duplicate element name '_id'. throw exeption InvalidOperationException: Duplicate element name '_id'. I want to get only _id list, other fileds not needed. 我想获得_id列表,不需要其他文件。 Documents may have different structures and exclude all other fileds manualy difficult. 文档可能具有不同的结构,并且排除所有其他文件,这些文件很难。

What C# query corresponds to the specified mongodb query db.getCollection('Documents').find({},{_id : 0, _id: 1} ? 什么C#查询对应于指定的mongodb查询db.getCollection('Documents').find({},{_id : 0, _id: 1}

UPDATE: Do not offer solutions related query large amounts of data from the server, for example like 更新:不提供与服务器相关的查询大量数据的解决方案,例如

this.Collection.Find(d => true).Project(d => d.Id).ToListAsync().Result;

Since your using C# driver I would recommend to use the AsQueryable and then use linq instead. 由于您使用C#驱动程序,我建议使用AsQueryable然后使用linq。

In my opinion it is better since you wouldn't need the magic strings and you would benefit from your linq knowledge. 在我看来,它更好,因为你不需要魔法字符串,你将从你的linq知识中受益。 Then it would look something like this 然后它看起来像这样

database.GetCollection<T>("collectionname").AsQueryable().Select(x => x.Id);

Alexey is correct, solutions such as these 阿列克谢是正确的,这些解决方案

var result = (await this.Collection<Foos>
                       .Find(_ => true)
                       .ToListAsync())
             .Select(foo => foo.Id);

Will pull the entire document collection over the wire, deserialize, and then map the Id out in Linq To Objects, which will be extremely inefficient. 将通过网络拉出整个文档集合,反序列化,然后将Lin中的Id映射到对象,这将是非常低效的。

The trick is to use .Project to return just the _id keys, before the query is executed with .ToListAsync() . 诀窍是在使用.ToListAsync()执行查询之前使用.Project只返回_id键。

You can specify the type as a raw BsonDocument if you don't want to use a strongly typed DTO to deserialize into. 如果您不想使用强类型DTO反序列化,则可以将类型指定为原始BsonDocument

var client = new MongoClient(new MongoUrl(connstring));
var database = client.GetDatabase(databaseName);
var collection = database.GetCollection<BsonDocument>(collectionName);
var allIds = (await collection
        .Find(new BsonDocument()) // OR (x => true)
        .Project(new BsonDocument { { "_id", 1 } })
        .ToListAsync())
    .Select(x => x[0].AsString);

Which executes a query similar to: 其执行类似于以下的查询:

db.getCollection("SomeCollection").find({},{_id: 1})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mongodb C#驱动程序-更新文档,然后选择包含更新文档的集合 - Mongodb C# driver - Update document then select collection with updated document 使用mongodb c#驱动程序从文档内部的集合中排除项目 - Exclude items from a collection inside a document with mongodb c# driver C# MongoDb Driver Filter 使用集合中的嵌套属性 - C# MongoDb Driver Filter using nested property from collection MongoDB C#驱动程序-序列化集合到接口 - MongoDB C# driver - Serialize collection to interface MongoDB C#驱动程序重命名集合 - MongoDB C# driver rename collection MongoDB C#驱动程序 - 从控制器返回类型字符串的Id - MongoDB C# Driver - Return an Id of type string from controller MongoDB c#驱动程序_id字段和SetIdMember() - MongoDB c# driver _id field and SetIdMember() 使用 C# 驱动程序(MongoDB)更新完整文档(如果“Id”存在则替换所有文档//如果“Id”不存在则插入) - Upsert a complete document (replace all if "Id" exists // insert if "Id" doesn't exists ) with C# Driver (MongoDB) 有没有一种简单的方法可以从 C# 中的 MongoDB 集合中删除所有数据? - Is there an easy way to remove all data from MongoDB collection in C#? UpdateOneModel,带有 Upsert - 错误 _id Mongodb C# MongoDB.Driver - UpdateOneModel, with Upsert - error _id Mongodb C# MongoDB.Driver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM