Why are writes failing in MongoDB?

Posted on

Question :

Here is the information about my development environment:

  • Microsoft Visual Studio Community 2015
  • .NET Framework 4.6
  • MVC assembly System.Web.Mvc Version=
  • MongoDB.Driver
  • Mongodb 3.0.6

From my ASP.NET C# application, I have code that writes to a MongoDB database.

Here is an excerpt from the logging shown by MongoDB after I started it in DOS command Prompt(From my analysis of the log shown below, it seems that a write to the database has occurred):

C:Program FilesMongoDBServer3.0bin>mongod --dbpath ./data/db
2015-09-25T14:18:04.567+0530 I JOURNAL  [initandlisten] journal     dir=./data/dbjo
2015-09-25T14:18:04.571+0530 I JOURNAL  [initandlisten] recover : no     journal fil
es present, no recovery needed
2015-09-25T14:18:05.120+0530 I JOURNAL  [durability] Durability thread     started
2015-09-25T14:18:05.122+0530 I JOURNAL  [journal writer] Journal writer     thread s
2015-09-25T14:18:05.297+0530 I CONTROL  [initandlisten] MongoDB starting :     pid=8
532 port=27017 dbpath=./data/db 64-bit host=My-PC
2015-09-25T14:18:05.298+0530 I CONTROL  [initandlisten] targetMinOS:     Windows 7/W
indows Server 2008 R2
2015-09-25T14:18:05.299+0530 I CONTROL  [initandlisten] db version v3.0.6
2015-09-25T14:18:05.299+0530 I CONTROL  [initandlisten] git version:     1ef45a23a4c
2015-09-25T14:18:05.300+0530 I CONTROL  [initandlisten] build info: windows     sys.
getwindowsversion(major=6, minor=1, build=7601, platform=2,     service_pack='Servic
e Pack 1') BOOST_LIB_VERSION=1_49
2015-09-25T14:18:05.301+0530 I CONTROL  [initandlisten] allocator: tcmalloc
2015-09-25T14:18:05.302+0530 I CONTROL  [initandlisten] options: { storage:     { db
Path: "./data/db" } }
2015-09-25T14:18:05.357+0530 I NETWORK  [initandlisten] waiting for     connections
on port 27017
2015-09-25T14:19:25.218+0530 I NETWORK  [initandlisten] connection accepted     from #1 (1 connection now open)
2015-09-25T14:19:36.317+0530 I NETWORK  [initandlisten] connection accepted     from #2 (2 connections now open)
2015-09-25T14:19:38.761+0530 I COMMAND  [conn2] command foo.$cmd command:     insert
 { insert: "Users", ordered: true, documents: [ { _id: 0, UserName: "blah",     Pass
word: "blahb", Email: "blha", PhoneNo: null, Address: null } ] }     keyUpdates:0 wr
iteConflicts:0 numYields:0 reslen:166 locks:{ Global: { acquireCount: { r:     1, w:
 1 } }, MMAPV1Journal: { acquireCount: { w: 2 }, acquireWaitCount: { w:     1 }, tim
eAcquiringMicros: { w: 38 } }, Database: { acquireCount: { w: 1 } },     Collection:
 { acquireCount: { W: 1 } } } 128ms
2015-09-25T14:22:01.148+0530 I NETWORK  [conn1] end connection (
1 connection now open)
2015-09-25T14:22:01.149+0530 I NETWORK  [conn2] end connection (
1 connection now open)
2015-09-25T14:22:54.252+0530 I NETWORK  [initandlisten] connection accepted     from #3 (1 connection now open)

Using another DOS Command Prompt, I ran the client Mongo in order to query the database:

C:Program FilesMongoDBServer3.0bin>mongo

MongoDB shell version: 3.0.6

connecting to: test

> use foo

switched to db foo

> db.Users.find()

{ "_id" : 
0, "UserName" : "quit", "Password" : "end", "Email" : "exit", "PhoneNo

" : null, "Address" : null }

> db.runCommand( { getLastError: 1, w: 1, wtimeout:5000 } )


     "connectionId" : 4,

     "n" : 0,

     "syncMillis" : 0,

     "writtenTo" : null,

     "err" : null,

     "ok" : 1



Based on the query results from the client Mongo, it seems the write to the database occurred.

Why is the MongoDB log stating that a write occurred, but the client Mongo fails to show any write occurred?
Also, could someone please tell me how to ensure that the entry gets written to MongoDB?

Answer :

The problem was in my C# Model POCO class code. ( https://stackoverflow.com/questions/32733038/why-is-c-sharp-and-mongodb-driver-code-connecting-but-failing-to-write/32798031#32798031 )

It took a while, but the problem was caused by the fact that I mistakenly used the int basic data type for the ID in the UserModel as opposed to ObjectId.

Here is the corrected code for the UserModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using WebApplication1.Models;
using MongoDB.Bson.Serialization.Attributes;

namespace WebApplication1.Models
    public class UserModel

        public ObjectId ID { get; set; }

        public string UserName { get; set; }

        public string Password { get; set; }

        public string Email { get; set; }

        public string PhoneNo { get; set; }

        public string Address { get; set; }

Leave a Reply

Your email address will not be published. Required fields are marked *