MongoDB 数据库作为Nosql中的一员,是非关系数据库当中功能最丰富,最像关系数据库的。
查询是比较方便的,通过QueryDocument 对象我们可以自由组装查询过滤条件!
今天在使用QueryDocument 的时候有个字段我用作条件过滤一直没有返回结果,代码如下:
string sObjID = "";
sObjID = tb_ObjID.Text;
QueryDocument query = new QueryDocument();
if (sObjID != "")
{
query.Add("ObjID", sObjID);
}
mongoDB.GetCollection().FindAs<BsonDocument>(query)
通过调试我发现数据库存入的时候是Int类型,难道query.Add("ObjID", sObjID)时候的sObjID必须也要转换成int类型?
下面是数据库集合中“字段”类型截图:
果然,我将前端参数设置成Int类型后就可以正常查询了!下面是我调整后的代码!
int iObjID = 0;
QueryDocument query = new QueryDocument();
if (iObjID != 0)
{
query.Add("ObjID", iObjID);
}
mongoDB.GetCollection().FindAs<BsonDocument>(query)
这本事一个小问题,刚用MongoDB 还不清楚原因为什么是这样,留个帖子先!
附上一些QueryDocument 的常用查询:
/*---------------------------------------------
* 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);