Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Java Error loading BSON file with $ref
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
PCimino  
View profile  
 More options Apr 26 2012, 8:58 am
From: PCimino <pcim...@gmail.com>
Date: Thu, 26 Apr 2012 05:58:14 -0700 (PDT)
Local: Thurs, Apr 26 2012 8:58 am
Subject: Java Error loading BSON file with $ref
Programatically created a an object with a reference. I can see and
export the data. In Java, I can load and reload the data from MongoDB.

Now I want to read it back in from a BSON file for unit tests.

The com.mongodb.DBCollection.validateKey() is throwing
java.lang.IllegalArgumentException: fields stored in the db can't
start with '$' (Bad Key: '$ref')

Objects look like this:

Parent:
{
     "_id" : ObjectId("4f97f90b697eadc853dc0b4c"),
     "_class" : "com.myorg.CustomerDetails",
           "customerAddress" : {
                "$ref" : "customerAddress",
                "$id" : ObjectId("000000000000000000000011")
           }

}

Reference Object:
{
         "_id" : ObjectId("000000000000000000000011"),
          "_class" : "com.myorg.CustomerAddress",


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sid  
View profile  
 More options Apr 26 2012, 11:34 am
From: Sid <siddharth.si...@10gen.com>
Date: Thu, 26 Apr 2012 08:34:14 -0700 (PDT)
Local: Thurs, Apr 26 2012 11:34 am
Subject: Re: Java Error loading BSON file with $ref
Mongo Object fields hold $ prefixed fields. This is for operations and
internal working.

You would need to remove these $ symbols before using the fields
within the DB since $ for fields would be ambiqous with operations and
Mongo would not be able to tell the difference.

On Apr 26, 8:58 am, PCimino <pcim...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Scott Hernandez  
View profile  
 More options Apr 26 2012, 11:36 am
From: Scott Hernandez <scotthernan...@gmail.com>
Date: Thu, 26 Apr 2012 08:36:21 -0700
Local: Thurs, Apr 26 2012 11:36 am
Subject: Re: [mongodb-user] Java Error loading BSON file with $ref
How are you building that object? Are you parsing it from a string? If
so, are you using the JSON class in the driver?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
PCimino  
View profile  
 More options Apr 26 2012, 11:54 am
From: PCimino <pcim...@gmail.com>
Date: Thu, 26 Apr 2012 08:54:42 -0700 (PDT)
Local: Thurs, Apr 26 2012 11:54 am
Subject: Re: Java Error loading BSON file with $ref
Remove them from where?

If I remove them from the BSON file, then what happens is the MongoDB
gets loaded, but the parent contains an instance of the child, all
attributes are null except the id and ref fields.

If I create the parent in Java, the child attribute has the @DBRef
annotation, store an instance of the child and then save from Java,
then MongoDB contains the objects shown above.

Yes I'm building from a string, I'll look into the driver being used,
I believe its the Mongo Java Driver. Or is there a JSON parsing class
in that package?

On Apr 26, 11:34 am, Sid <siddharth.si...@10gen.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
PCimino  
View profile  
 More options Apr 26 2012, 11:58 am
From: PCimino <pcim...@gmail.com>
Date: Thu, 26 Apr 2012 08:58:50 -0700 (PDT)
Local: Thurs, Apr 26 2012 11:58 am
Subject: Re: Java Error loading BSON file with $ref
Mongo Java Driver. The issue desn't appear to be the driver pushing/
pulling to/from MongoDB. Its the parsing of the BSON file.

db is a refenece to the Mongo database
br is a BufferedReader to the BSON file
[code]
 while ((thisLine = br.readLine()) != null) {
  Map<String, Object> objectInMap =
mapper.readValue(thisLine.getBytes(), new TypeReference<Map<String,
Object>>() {});
  BasicDBObjectBuilder b = BasicDBObjectBuilder.start(objectInMap);
  DBObject dbObject = b.get();
  BasicDBObject dbObject = new BasicDBObject(objectInMap);
   db.getCollection(collection).insert(dbObject);

}

[/code]

On Apr 26, 11:36 am, Scott Hernandez <scotthernan...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Scott Hernandez  
View profile  
 More options Apr 26 2012, 12:03 pm
From: Scott Hernandez <scotthernan...@gmail.com>
Date: Thu, 26 Apr 2012 09:03:19 -0700
Local: Thurs, Apr 26 2012 12:03 pm
Subject: Re: [mongodb-user] Re: Java Error loading BSON file with $ref
That isn't BSON, it is JSON, which is fairly different.

How are you generating the data you use the BufferedReader to read?
Where is it coming from?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
PCimino  
View profile  
 More options Apr 26 2012, 2:15 pm
From: PCimino <pcim...@gmail.com>
Date: Thu, 26 Apr 2012 11:15:51 -0700 (PDT)
Local: Thurs, Apr 26 2012 2:15 pm
Subject: Re: Java Error loading BSON file with $ref
Mongodump to a file, then bsondump to get the file into bson format.
The file basically looks like what I already posted, had to take out
the "Object()" class identifier t get things to work.

{ "_id" : "4f97f90b697eadc853dc0b4c",  "_class" :
"com.myorg.CustomerDetails", "name" : "my parent", "customerAddress" :
{  "$ref" : "customerAddress",  "$id" :
"000000000000000000000011")} }

{  "_id" : "000000000000000000000011",  "_class" :
"com.myorg.CustomerAddress", "name" : "my child"}

On Apr 26, 12:03 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Scott Hernandez  
View profile  
 More options Apr 26 2012, 3:06 pm
From: Scott Hernandez <scotthernan...@gmail.com>
Date: Thu, 26 Apr 2012 12:06:47 -0700
Local: Thurs, Apr 26 2012 3:06 pm
Subject: Re: [mongodb-user] Re: Java Error loading BSON file with $ref
If you have BSON files you can load their binary contents directly
into DBObjects in java. This will bypass the BSON->JSON (string) ->
??? -> JAVA (DBOjbect) conversion, which is causing the problems you
are seeing, and will be much faster as well.

Here is a snippet which you can work from:
        InputStream in = new FileInputStream("somefile.bson");
        DBDecoder decoder = new DefaultDBDecoder();
        while(in.available() > 0) {
            DBObject dbObj = decoder.decode( in, (DBCollection) null );
            System.out.println(dbObj.toString());
        }


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
PCimino  
View profile  
 More options Apr 27 2012, 7:22 am
From: PCimino <pcim...@gmail.com>
Date: Fri, 27 Apr 2012 04:22:58 -0700 (PDT)
Local: Fri, Apr 27 2012 7:22 am
Subject: Re: Java Error loading BSON file with $ref
Thanks. That does work.

Ideally we want files to be flat files so they can be edited directly
to create test data..

Seems like we might have to have fixtures to create test data in Mongo
and then export DBs when we want to reinitialize a new DB. Like to
eliminate the extra step if possible. We don't want to write code to
create test data to test code.

On Apr 26, 3:06 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Scott Hernandez  
View profile  
 More options Apr 27 2012, 9:49 am
From: Scott Hernandez <scotthernan...@gmail.com>
Date: Fri, 27 Apr 2012 06:49:55 -0700
Local: Fri, Apr 27 2012 9:49 am
Subject: Re: [mongodb-user] Re: Java Error loading BSON file with $ref
In that case you probably want to store them as json and use the
JSON.parse(...) to load them back. Please not that not all data will
round-trip via json to the same resulting data. Numbers for example
may change from int32/64/double depending on how they are parsed and
their values.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »