mongodb查询仅在.NET代码中获取字段
任何人都可以帮助获得以下查询的正确.NET C或VB.NET:mongodb查询仅在.NET代码中获取字段,.net,json,vb.net,extjs4,mongodb-.net-driver,.net,Json,Vb.net,Extjs4,Mongodb .net Driver,任何人都可以帮助获得以下查询的正确.NET C或VB.NET: > db.usercollection.find( {}, { username:1, _id: 0 } ) { "username" : "testuser1" } { "username" : "testuser2" } { "username" : "testuser3" } 基本上我只想返回文档的某些字段。另外,mongodb c驱动程序将结果转换为json格式,可以填充网格extjs网格或图表,而
> db.usercollection.find( {}, { username:1, _id: 0 } )
{ "username" : "testuser1" }
{ "username" : "testuser2" }
{ "username" : "testuser3" }
基本上我只想返回文档的某些字段。另外,mongodb c驱动程序将结果转换为json格式,可以填充网格extjs网格或图表,而无需我显式地进行转换
Using mongo.RequestStart(db)
Dim collection = db.GetCollection(Of BsonDocument)("usercollection").FindAll()
Dim collection3 = db.GetCollection(Of BsonDocument)("usercollection").
Find({}, {"username:1", "_id:0"})
collection3行不正确
还尝试了以下操作:
For Each ruleSet In collection
Dim rules As String = ruleSet.GetValue(0).AsString
response = rules & response
Next
但这会产生错误:
UNABLE TO CAST OBJECT OF TYPE 'MONGODB.BSON.BSONOBJECTID TO TYPE MONGODB.BSON.BSONSTRING
如果要将结果限制为特定字段,则需要在MongoCurs上使用SetFields或类似的方法:
非常感谢。还有一个问题:如何仅限制用户名而不包括_-id objectId…d=[{u-id:objectid52d2f2b3c6084b25bc5dc5ca,用户名:testuser1},{u-id:objectid52d2f9c6084b25bc5cb,用户名:testuser2},{u-id:objectid52d2f9c6084b25bc5bc5bc5cc,用户名:testuser3}]@vbNewbie您的意思是只想查询用户名字段,而不想查询该文档的_id?是的,先生。可能吗?我需要将这些数据映射到网格,并且希望忽略_id。我认为最好在客户端这样做。我不确定它是否可以在db中完成。这里:cursor.Selectdocument=>document[username]您的意思是从结果集中分离出我需要的数据吗?
MongoCursor<BsonDocument> cursor = _db.GetCollection<BsonDocument>.FindAll(); // or any other query
cursor.SetFields("username");
BsonDocument document;
document.ToJson();