MongoDB not throwing error when using find on non-existent collection

366 views
Skip to first unread message

mec0n...@gmail.com

unread,
Nov 16, 2014, 8:17:59 AM11/16/14
to mongod...@googlegroups.com
Hello,

When I try to findOne(...) on a non-existent collection MongoDB does not throw an error!

Here is the code I am using:

socket.on('login', function (details) {
        dbHandle
.collection('Users', function(err, logins) {
           
if (err) throw err ;
            logins
.findOne({'user': details.User, 'pwd': details.Pass }, function(err, record) {
               
if (err) {
                    console
.log('Oops!') ;
                    socket
.emit('error', { error: 'Oops'} ) ;
               
}
               
else
               
{
// I always get this returned to client!
                    socket
.emit('login', {data: 'record'} ) ;
               
}
           
});
       
}) ;
   
}) ;

I am using MongoDB v2.6.5 on Node.JS v0.10.33 on Linux.

I would appreciate your thoughts on this as I can't see what or where I'm going wrong.

--
Bill

Sam Millman

unread,
Nov 16, 2014, 8:36:30 AM11/16/14
to mongod...@googlegroups.com
MongoDB creates collections lazily and on-demand, it would be incorrect behaviour to throw an error here, remember MongoDB is not a strict schema database like SQL techs.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/225ff047-ac8b-4b90-94e2-54d3b706e5c6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sam Millman

unread,
Nov 16, 2014, 8:37:59 AM11/16/14
to mongod...@googlegroups.com
Ha hang on you mean on non-existant row not collection

mec0n...@gmail.com

unread,
Nov 16, 2014, 9:03:18 AM11/16/14
to mongod...@googlegroups.com
Hi, thanks for the reply.

I am aware of the laziness which is why I was expecting MongoDB to throw an error when it tried to search. But having said that if the collection is then created lazily then there is something to search isn't there!

So apart from my 'record' being null is there any way to determine if a search fails?

--
Bill


Stephen Steneker

unread,
Nov 16, 2014, 5:36:38 PM11/16/14
to mongod...@googlegroups.com
On Monday, 17 November 2014 01:03:18 UTC+11, mec0n...@gmail.com wrote:
I am aware of the laziness which is why I was expecting MongoDB to throw an error when it tried to search. But having said that if the collection is then created lazily then there is something to search isn't there!

So apart from my 'record' being null is there any way to determine if a search fails?

Hi Bill,

I don't believe there is any way to differentiate between a search that doesn't return any results because the collection doesn't exist vs lack of results due to no matches. For most use cases the outcome is semantically equivalent.

If you need certain collections to exist (for example, because they have default data), this sounds like something you probably want to ensure on startup rather than on each request.

You could check a collection exists via collectionNames in the Node driver:

Regards,
Stephen

Con Fused

unread,
Nov 20, 2014, 5:27:59 AM11/20/14
to mongod...@googlegroups.com
Hi Stephen,

I guess all of that makes sense. I'll have a look at the collectionNames later.

--
Bill

Reply all
Reply to author
Forward
0 new messages