Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB C# Get all documents from a list of IDs

Tags:

c#

.net

mongodb

I have a list of Ids

List<string> Ids;

and I would like to retrieve all the documents matching these Ids.

There are solutions on the web:

var ids = new int[] {1, 2, 3, 4, 5};
var query = Query.In("name", BsonArray.Create(ids));
var items = collection.Find(query);

but they're all with the old C# driver and with the (not so new anymore) 2.2.4 driver the API has changed and I can't find how to build this query.

like image 253
Thomas Avatar asked Jun 13 '16 11:06

Thomas


People also ask

What is MongoDB C driver?

The MongoDB C Driver, also known as “libmongoc”, is a library for using MongoDB from C applications, and for writing MongoDB drivers in higher-level languages. It depends on libbson to generate and parse BSON documents, the native data format of MongoDB.

Is MongoDB a C++?

Welcome to the documentation site for the official MongoDB C++ driver. You can add the driver to your application to work with MongoDB using the C++11 or later standard.

Is MongoDB free to use?

MongoDB is a NoSQL database that is open source. MongoDB is available in two editions. One is MongoDB Open Source, which is free as part of the Open-Source Community, but for the other editions, you must pay a License fee. When compared to the free edition, this edition has some advanced features.

What is Mongo-C in C?

MongoDB C-driver, formerly known as mongo-c, is a project comprising two libraries: libmongoc, a client library that has been written in C for MongoDB. libbson, a library that offers practical routines related to building, parsing, and iterating BSON documents, the native data format of MongoDB.

What is the MongoDB C driver?

The MongoDB C Driver, also known as "libmongoc", is the official client library for C applications, and provides a base for MongoDB drivers in higher-level languages. The library is compatible with all major platforms.

What is libmongoc in MongoDB?

The MongoDB C Driver, also known as “libmongoc”, is a library for using MongoDB from C applications, and for writing MongoDB drivers in higher-level languages. It depends on libbson to generate and parse BSON documents, the native data format of MongoDB.

What is MongoDB used for?

MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era. No database makes you more productive. Try MongoDB free in the cloud! As a programmer, you think in objects. Now your database does too. MongoDB is a document database, which means it stores data in JSON-like documents.


1 Answers

please see snippet below (made using LINQPad)

void Main()
{
    // To directly connect to a single MongoDB server
    // or use a connection string
    var client = new MongoClient("mongodb://localhost:27017");
    var database = client.GetDatabase("test");


var collectionEmpInfo = database.GetCollection<Employee>("Employee");
Employee EmpInfo = new Employee
{

    EmpID = "103",
    EmpName = "John",
    CreatedAt = DateTime.Now,
    EmpMobile = new List<Mobile>
    {
        new Mobile{ MobNumber = "55566610", IsPreferred = true, MobID = ObjectId.GenerateNewId() },
        new Mobile{ MobNumber = "55566611", IsPreferred = false, MobID = ObjectId.GenerateNewId() },
    }
};
//collectionEmpInfo.InsertOne(EmpInfo);

var filterDef = new FilterDefinitionBuilder<Employee>();
var filter = filterDef.In(x=>x.EmpID , new[]{"101","102"});
filter.Dump();
var empList = collectionEmpInfo.Find(filter).ToList();
empList.Dump();
}
public class Employee
{
   public ObjectId Id  { get; set; }
    public string EmpID { get; set; }
    public string EmpName { get; set; }
    public List<Mobile> EmpMobile { get; set; }
    public DateTime CreatedAt { get; set; }
}

public class Mobile
{
    public ObjectId MobID { get; set; }
    public string MobNumber { get; set; }
    public bool IsPreferred { get; set; }
}

and results screenshot

linquPaDscreenShot

like image 158
profesor79 Avatar answered Sep 23 '22 03:09

profesor79