Bad BSON Document: illegal CString with Node MongoDB driver - node.js

I'm receiving:
[Error: Bad BSON Document: illegal CString]
When using the Node MongoDB driver while iterating over one of my collections with Cursor.each. It seems to make some of my documents disappear, and not found in the collection, even though they were individually accessible when I look for them using Collection.findOne().
Does this mean that my data is corrupted in some way?

Thanks to #wdberkeley for all the help in the above comment, which helped me to track down my problem.
It turns out that I did have a single corrupted document in my collection, which was inserted during an unclean shutdown of Mongo. I was unaware how that document would affect the rest of my queries though.
When you perform a collection.find(), and then start iterating with the cursor over the collection, the cursor will stop and be unable to go any further if it encounters an error, such as with [Error: Bad BSON Document: illegal CString].
This happens with both cursor.forEach or cursor.nextObject. Thus, I was unable to access any of the documents that came after the error in the collection, even though I was able to access those documents individually with collection.findOne.
The only solution in this scenario for me was to run db.repairDatabase, which removed the corrupted documents, and solved the problem for me.

Related

Unable to use Code data type in Mongoose Schema

I wanted to store a data type of Code in my MongoDB database per the docs.
The issue is that when I use Mongoose to specify this type, I get an error:
ReferenceError: Code is not defined
And yes, Mongoose's doc's don't have a type of Code.
Say I want to save Javascript code in my MongoDB database, how could I do so?
For more insight into why I am doing this: I want to save Puppeteer scripts in the database, and using the FS module add them to a file, execute them and send the results as a response object back to the client.
Right now, the code is saved as String, which seems to cause execution errors.
For more information, please don't hesitate to ask.
Thank you,

Resource Conflict after syncing with PouchDB

I am new to CouchDB / PouchDB and until now I somehow could manage the start of it all. I am using the couchdb-python library to send initial values to my CouchDB before I start the development of the actual application. Here I have one database with templates of the data I want to include and the actual database of all the data I will use in the application.
couch = couchdb.Server()
templates = couch['templates']
couch.delete('data')
data = couch.create('data')
In Python I have a loop in which I send one value after another to CouchDB:
value = templates['Template01']
value.update({ '_id' : 'Some ID' })
value.update({'Other Attribute': 'Some Value'})
...
data.save(value)
It was working fine the whole time, I needed to run this several times as my data had to be adjusted. After I was satisfied with the results I started to create my application in Javascript. Now I synced PouchDB with the data database and it was also working. However, I found out that I needed to change something in the Python code, so I ran the first python script again, but now I get this error:
couchdb.http.ResourceConflict: (u'conflict', u'Document update conflict.')
I tried to destroy() the pouchDB database data and delete the CouchDB database as well. But I still get this error at this part of the code:
data.save(value)
What I also don't understand is, that a few values are actually passed to the database before this error comes. So some values are saved() into the db.
I read it has something to do with the _rev values of the documents, but I cannot get an answer. Hope someone can help here.

List recent operations on mongodb

I'm using MongoDB with Node.js framework.
There is a weird behavior that some documents are not getting inserted into db, though from orm's point of view there are no errors: err = null in callback of Collection.create() and fresh document with _id is returned. When I try to search by that _id in db - no document is found.
I tried to manually insert new document to db and it was successfull.
Is there a way I can trace these operations from db's point of view? Some command to list recent requests and their results..?
You can enable profiling for all operations:
db.setProfilingLevel(2)
Then, look at system.profile collection to see what's happen. system.profile is a capped collection that can be searched as any other collection. Profiling can be noisy, and eventually you should have to change the size of the system.profile collection
db.setProfilingLevel(0)
db.system.profile.drop()
db.createCollection( "system.profile", { capped: true, size:4000000 } )
db.setProfilingLevel(2)
The most notable way of tracking errors within MongoDB is to use the --diaglog option: http://docs.mongodb.org/manual/reference/program/mongod/#cmdoption--diaglog with maybe a level of 3, however 1 might be enough for you.
As noted by #Neil this has unfortunately become deprecated as of 2.6.
The only way currently is to write out ALL operations MongoDB performs, via #Rauls answer, and then use a query like:
db.system.profile.find({op:{$in:['update', 'insert', 'remove']}});
and possibly resize the capped collection used for profiling: http://docs.mongodb.org/manual/tutorial/manage-the-database-profiler/#profiler-overhead to capture the amount you want.

Model.create does not work on very large array of documents

I am facing a problem with mongoose Model.create method.
When i call
Model.create(arrayOfThousandDocs, function(err){});
After 15 min (sufficient for all the docs to get saved) when i switch to mongo shell and query upon total no of docs saved
then i find only something around 700-800 (no of docs saved varies every time Model.create is called).
And mongoose or mongo returns no any error.
Have anyone faced the same bug?
Please tell me how to resolve it.
it may be because, Model.create uses forEach method, and that is not suitable in nodejs asynchronous mode programming.. Please correct me if if i am wrong..
and suggest your views..
here is the source : http://mongoosejs.com/docs/api.html#model_Model.create

Collection name in Mongoose

Why would a database named 'blog' not allow a record insert and also give no return error and why would a database named 'blogs' allow a record inserts and return errors?
I just spent several hours going through all my code thinking I did something wrong. I have written many mongoose connected apps but when using the following it would return success but not insert the record and return no error as to why:
mongooose.connect('mongodb://localhost:27017/blog');
After banging my head against a wall for a bit I decided to change the database name:
mongooose.connect('mongodb://localhost:27017/blogs');
It works! But why would this name convention matter? I can't find anything in the documentation for MongoDB or Mongoosejs.
So I'm fairly certain mongodb doesn't care about database name "blog" vs "blogs". However, do note that mongoose has the questionably-helpful feature of silently queueing up operations while the database connection is still not established and then firing them off if/when the database connection is ready. That could be causing your confusion. To test that theory, pass a callback to mongoose.connect and put a console.log in the callback so you know exactly when the connection is ready.

Resources