QueryDocument 的常用查询

QueryDocument 的常用查询:

[csharp] view plain copy

/** sql : SELECT * FROM table WHERE ConfigID > 5 AND ObjID = 1 

*--*/              

QueryDocument query = new QueryDocument();              

BsonDocument b = new BsonDocument();             

b.Add("$gt", 5);                         

query.Add("ConfigID", b);  

query.Add("ObjID", 1);  

MongoCursor<Person> m = mongoCollection.FindAs<Person>(query);  

  /*             

* sql : SELECT * FROM table WHERE ConfigID > 5 AND ConfigID < 10            

*              

*/              

QueryDocument query = new QueryDocument();              

BsonDocument b = new BsonDocument();              

b.Add("$gt", 5);     

b.Add("$lt", 10);                   

query.Add("ConfigID", b);  

MongoCursor<Person> m = mongoCollection.FindAs<Person>(query); 

 

public class News
 {
       public int _id { getset; }
       public int count { getset; }
       public string news { getset; }
       public DateTime time { getset; }

}

 MongoCursor<BsonDocument> allDoc = coll.FindAllAs<BsonDocument>();
BsonDocument doc = allDoc.First(); //BsonDocument类型参数
 MongoCursor<News> allNews = coll.FindAllAs<News>();
 News aNew = allNews.First(); //News类型参数
 News firstNews = coll.FindOneAs<News>(); //查找第一个文档
 QueryDocument query = new QueryDocument(); //定义查询文档
query.Add("_id"11);
query.Add("count"1);
MongoCursor<News> qNews = coll.FindAs<News>(query);
 
BsonDocument bd = new BsonDocument();//定义查询文档 count>2 and count<=4
bd.Add("$gt"2);
bd.Add("$lte"4);
QueryDocument query_a = new QueryDocument();
query_a.Add("count",bd);
FieldsDocument fd = new FieldsDocument();
fd.Add("_id"0);

fd.Add("count"1);
fd.Add("time"1);
MongoCursor<News> mNewss = coll.FindAs<News>(query_a).SetFields(fd);//只返回count和time
var time = BsonDateTime.Create("2011/9/5 23:26:00");
BsonDocument db_t = new BsonDocument();
db_t.Add("$gt", time);
QueryDocument qd_3 = new QueryDocument();
qd_3.Add("time", db_t);
MongoCursor<News> mNews = coll.FindAs<News>(qd_3);//

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java与MongoDB集成的方式有很多,其中一种常用的方法是使用MongoDB的Java驱动程序来进行游标查询。 游标查询是一种在MongoDB中用于获取大量数据的一种方式。与传统的查询方式不同,游标查询并不是一次性获取全部数据,而是按需获取数据,可以减轻对内存的开销。 在Java中使用游标查询,首先需要导入MongoDB的Java驱动程序包。然后,我们可以使用以下代码示例来进行游标查询: ``` // 创建MongoDB链接 MongoClient mongoClient = new MongoClient("localhost", 27017); // 获取数据库 MongoDatabase database = mongoClient.getDatabase("myDatabase"); // 获取集合 MongoCollection<Document> collection = database.getCollection("myCollection"); // 创建查询 Document query = new Document("name", "John"); // 执行查询 FindIterable<Document> cursor = collection.find(query); // 遍历游标结果 for (Document document : cursor) { System.out.println(document); } // 关闭游标和连接 cursor.close(); mongoClient.close(); ``` 在上面的代码中,我们首先创建了一个MongoDB的链接,然后获取了要查询的数据库和集合。接着,我们创建了一个查询文档,并通过`collection.find(query)`方法执行查询,得到一个游标。然后,我们可以使用`for`循环遍历游标的结果,并对每个结果进行相应的处理。最后,我们需要手动关闭游标和关闭MongoDB的链接。 游标查询是一种非常灵活和高效的查询方式,可以处理大量的数据。但是需要注意的是,在使用游标查询时,要避免返回大量的数据,以免对内存造成过大的压力。对于需要返回大量数据的情况,可以考虑使用分页查询的方式来进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值