Is MongoDB thread-safe?
Asked Answered
H

2

9

I'm running MongoDB on Windows. I have 1 or more threads that drop and recreate a collection.

Using mongo.exe with the show collections() command, I'm seeing multiple collections with the same name (well over 1,000 collections with the same name!).

When I run validate:

> db.MY_COLLECTION.validate()

I get:

{ "errmsg" : "ns not found", "ok" : 0, "valid" : false }

The size() command returns 0, and find() returns nothing.

My question is: Is MongoDB thread safe? A follow on question would be something like 'Am I doing this correctly (dropping and recreating) or is there a better way to refresh the whole content of a collection?'

Hysteric answered 4/7, 2011 at 17:37 Comment(0)
P
7

From mongodb documentation:

Thread safety

Only a few of the C# Driver classes are thread safe. Among them: MongoServer, MongoDatabase, MongoCollection and MongoGridFS. Common classes you will use a lot that are not thread safe include MongoCursor and all the classes from the BSON Library (except BsonSymbolTable which is thread safe). A class is not thread safe unless specifically documented as being thread safe.

All static properties and methods of all classes are thread safe.

You can search for the word Thread on this page:

http://mongodb.onconfluence.com/pages/viewpage.action?pageId=18907708&navigatingVersions=true#CSharpDriverTutorial-Threadsafety

Precursor answered 4/7, 2011 at 17:47 Comment(1)
Above link doesn't seem to be active anymore.Birdiebirdlike
D
5

Changed in version 2.2.

MongoDB allows multiple clients to read and write a single corpus of data using a locking system to ensure that all clients receive a consistent view of the data and to prevent multiple applications from modifying the exact same pieces of data at the same time. Locks help guarantee that all writes to a single document occur either in full or not at all.

http://docs.mongodb.org/manual/faq/concurrency/

Duhamel answered 27/12, 2012 at 5:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.