How to query List<BsonDocument>batch Mongo database using Field values/contents

94 views
Skip to first unread message

Henry

unread,
Jul 12, 2017, 6:11:36 PM7/12/17
to mongodb-user
Dear MongoDB-user group

Please I will like some help with the query of MongoCollection<BsonDocument> as given in the following link (which I have also pasted below). 


or 



Thank you for your help

Regards

Henry



Challenge as in stackoverflow

I am struggling with querying the resulting Mongo data imported from a DataTable as shown in the codes below. I can confirm that the data is present in mongo database as shown by the MessageBox output. However, I need some options of code snippet that can retrieve specific information e.g. particular rows - all entries in the row - (from 'batch' or 'collec' mongo components) based on column/field values such as 'var query = new QueryDocument("Column1", "Henry") or ' var filter = Builders<BsonDocument>.Filter.Eq("Column1", "Henry")'. The cases 'collec.Find(query).ToList()' does not pick any entry and 'collec.Find(Filter).ToList()' throws exceptions of invalid arguments in Find(...IMongoQuery) 
    MongoClient mongo = new MongoClient("mongodb://localhost");
    MongoServer server;
    MongoDatabase database;
    
          private void Form1_Load(object sender, EventArgs e)
            {  
              server = mongo.GetServer();
              server.Connect();
              database = server.GetDatabase("test");
              List<BsonDocument> batch = new List<BsonDocument>();            
                    foreach (DataRow dr in dt.Rows)
                {                               
                    var dictionary = dr.Table.Columns.Cast<DataColumn> ().ToDictionary(col => col.ColumnName, col => dr[col.ColumnName]);
                    batch.Add(new BsonDocument(dictionary));                    
                }
                    
                MongoCollection<MongoDB.Bson.BsonDocument> collec = database.GetCollection<BsonDocument>("test");
                
                collec.InsertBatch(batch); //// produces BsonIds for enteries                             
                               
                var results = batch.ToList();            
                string json = results.ToJson();
                MessageBox.Show(json);
              
              ////Part I am struggling with
              var query = new QueryDocument("Column1", "Henry");
              collec.Find(query).ToList(); // THIS DOES NOT PICK ANY RECORD FROM THE DATABASE (SHOWS [])
              var filter = Builders<BsonDocument>.Filter.Eq("Column1", "Henry");
              collec.Find(Filter).ToList()  // THIS ALSO THROWS INVALID ARGUMENT WRT FIND(...)
    
             }


Please examples of using these query options - 'collec.Find(query).ToList()' and 'collec.Find(Filter).ToList() - as mentioned above will be useful.THANKS


Kevin Adistambha

unread,
Jul 25, 2017, 3:44:02 AM7/25/17
to mongodb-user

Hi Henry

Do I understand correctly that you have solved this question?

As you may have noted in the linked StackOverflow question, MongoDB field names are case-sensitive. For example:

> db.test.find()
{
  "_id": ObjectId("5976f6202dbe1d773beb11ce"),
  "abc": 123
}
Fetched 1 record(s) in 1ms

> db.test.find({ABC:123})
Fetched 0 record(s) in 1ms

> db.test.find({abc:123})
{
  "_id": ObjectId("5976f6202dbe1d773beb11ce"),
  "abc": 123
}
Fetched 1 record(s) in 1ms

That is, a document with field names abc (all lowercase) will not be found if you specify your query with a different case (ABC in the above example).

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages