Sunday, November 17, 2019

Excluding fields in MongoDB using Asp.net



using MongoDB.Bson;
using MongoDB.Driver;                             
          
static MongoClient client = new MongoClient("mongodb://localhost:27017"); //Set the connection string

static IMongoDatabase db = client.GetDatabase("test"); //Database name

public void Exclude()
{
   IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user");

   var filter = Builders<BsonDocument>.Filter.Empty;   

 // uncomment the below two lines to exclude the required things

// var project = Builders<BsonDocument>.Projection.Exclude("_id"); //replace the field which you want to exclude in place of _id and it must match the field name in the MongoDB document

//var res = collection.Find<BsonDocument>(filter).Project(project).ToList();

    var res = collection.Find(filter).ToList(); //Comment this after un commenting the above two line

}

Before excluding _id field

After 



Saturday, November 16, 2019

Delete record from MongoDB using Asp.net



using MongoDB.Bson;
using MongoDB.Driver;   
                                    
static MongoClient client = new MongoClient("mongodb://localhost:27017"); //Set the connection string

static IMongoDatabase db = client.GetDatabase("test"); //Database name

public void DeleteOne(string id)
{
   IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user"); //table name

   var filter = Builders<BsonDocument>.Filter.Eq("_id",  BsonObjectId.Parse(id)); //Need to parse else you will get the error

   var res = collection.Find(filter).ToList();
 
   collection.DeleteOne(filter);
}

Filter record in MongoDB using Asp.net


using MongoDB.Bson;
using MongoDB.Driver;              
                         
static MongoClient client = new MongoClient("mongodb://localhost:27017"); //Set the connection string

static IMongoDatabase db = client.GetDatabase("test"); //Database name

public void ListOne(string age)
 {
    IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user");

    var filter = Builders<BsonDocument>.Filter.Eq("age", age);

    var res = collection.Find(filter).ToList();

 }

Get records from MongoDB using Asp.net.

To get the records from MongoDB using Asp.net.

using MongoDB.Bson;
using MongoDB.Driver;         
                              
static MongoClient client = new MongoClient("mongodb://localhost:27017"); //Set the connection string

static IMongoDatabase db = client.GetDatabase("test"); //Database name
     
 public void List()
  {

    IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user"); //table name

    var filter = Builders<BsonDocument>.Filter.Empty; //It behaves like select * from user

    var res = collection.Find(filter).ToList();
              
 }

Insert a record into MongoDB using Asp.net.


Below code shows how to insert a record into MongoDB using Asp.net.

First install the MongoDB driver from the nuget package manager.



using MongoDB.Bson;

using MongoDB.Driver; 

static MongoClient client = new MongoClient("mongodb://localhost:27017");//Set the connection string

        
static IMongoDatabase db = client.GetDatabase("test"); //Database name 


        public void InsertCollection()
        {
            BsonDocument doc = new BsonDocument  //Create BSON document object
            {
            { "name" , "me" },
            {"age" , "99"},
            {"addr" , new BsonDocument { {"pin" , "1234"} , {"state","unknown"} }}
            };

          IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user"); //Table name

            collection.InsertOne(doc);

        }

Request format is unrecognized for URL unexpectedly ending in '/'


Add the below code in the Web.config inside the system.web.


              <webServices>
                     <protocols>
                           <add name="HttpGet"/>
                           <add name="HttpPost"/>
                     </protocols>
              </webServices>


Git Commands

Git Version   To check the git version Git -v       Git Clone To clone the repository, use the following command: Git clone [u...