MongoDB Java Driver Dot Notation

127 views
Skip to first unread message

xtechgamer 735

unread,
Aug 27, 2017, 5:28:40 PM8/27/17
to mongodb-user
Hello!

I am having awful trouble trying to retrieve values using dot notation. I need to be able to do this since it relies on how user data is entered.

I am currently getting a document using this code 

collection.find(eq(field, value)).first();

However, when using the .get() function for a string such as "user.name" I just get null returned. The database is operational since reading and writing other data works as expected. I have read a lot of complaint online the the BasicDBObject class and children do not support dot notation and being new to mongo I can't for the life of me figure out a way around this.

If anyone could point me in the right direction of making this possible that would be great!

Many thanks,

Kevin Adistambha

unread,
Sep 5, 2017, 2:47:30 AM9/5/17
to mongodb-user

Hi

I am having awful trouble trying to retrieve values using dot notation.

Do you mean that the key part of a document e.g. {key: value} contains a dot?

If yes, then it’s not possible to create a document with a key with a dot in its name. For example, you cannot create a document like this (it will result in an error):

> db.test.insert({"user.name": "foo"})  //will result in an error

This is because MongoDB interprets dots as a special operator (see Dot Notation), which was designed to query a sub-document. For example:

> db.test.find({'user.name':'foo'})
{
  "_id": 1,
  "user": {
    "name": "foo"
  }
}

If this is not your intent, could you please provide more detail:

  • What is your MongoDB version, and your Java driver version
  • Some example documents
  • Some example results that you require
  • Any error message you see

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages