Can I do a text query with the mongodb c# driver

前端 未结 5 2082
萌比男神i
萌比男神i 2020-12-12 17:52

Is there a way to submit a query that is expressed in the shell query syntax to the mongo c# driver

For example Something like

Coll.find { \"myrecs\"         


        
5条回答
  •  我在风中等你
    2020-12-12 18:09

    Using the official C# driver, you'd do something like this:

    var server = MongoServer.Create("mongodb://localhost:27017");
    var db = server.GetDatabase("mydb");
    var col = db.GetCollection("col");
    
    var query = Query.And(Query.EQ("x", 3), Query.EQ("y", "abc"));
    var resultsCursor = col.Find(query).SetSortOrder("x");
    var results = resultsCursor.ToList();
    

    The equivalent query from the shell would be:

    col.find({ x: 3, y: "abc" }).sort({ x: 1 })
    

提交回复
热议问题