0

Hi i just read about WriteConcern in MongoDB and want to apply in my code as well all i am doing is this

val mYdb = client("myDB")//get database Name
    val collection = mYdb("directUser")//get collection Name

    collection.drop()

       val result:WriteResult= collection.insert(new BasicDBObject("_id",directuser.uuid)
                        .append("Email",directuser.email)
                        .append("SecondryEmail",directuser.secondryEmail)
                        .append("FirstName",directuser.firstName)
                        .append("LastName",directuser.lastName)
                        .append("UserStatus",directuser.userStatus.toString())
                        ,WriteConcern.Acknowledged)

and from this link the deffination of

Acknowledged With a receipt acknowledged write concern, the mongod confirms that it received the write operation and applied the change to the in-memory view of data. Acknowledged write concern allows clients to catch network, duplicate key, and other errors.

how can i achieve the returned values and also want to get the confirmation

That it received the write operation and applied the change to the in-memory view of data.

how can i get to know that data is inserted successfully and no erros occured in insertion ..please help

swaheed
  • 3,671
  • 10
  • 42
  • 103

1 Answers1

0

As the documentation you provided says, with an Acknowledged write the server responds to the client once the insert has been applied in memory. Once the insert is applied in memory it is "successful" and will only be "lost" if the server holding the data crashes before the document is replicated or the journal and/or data files are written to disk.

With the Java client the insert(...) will throw an exception if the write fails. This means that if the insert(...) does not throw an exception then, with WriteConcern.Acknowledged, the documents are now in memory on the server and is successful. The WriteResult also contains the metrics on the results of the write (although with pre-2.6 servers the numbers will probably be zero, but a failure would still have thrown an exception).

If you need the write to be more durable than simply in the server's memory there are other WriteConcern levels you can use with a general trade off in higher latency leading to a more durable write. See the Javadoc for details.

HTH - Rob

Rob Moore
  • 3,343
  • 17
  • 18
  • can you please explain via code how to use WriteResult's to confirm success – swaheed Apr 13 '15 at 06:35
  • You don't need to look at the WriteResult to tell if the insert succeeded. If there is no exception then the write succeeded. – Rob Moore Apr 14 '15 at 02:00