[JAVA-927] "deep copy" for Document Created: 14/Aug/13  Updated: 07/Mar/17  Resolved: 21/Sep/16

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Alexander Azarov Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Can you please enhance API to provide `deep copy` for a Document object.

It was possible in version 2.x of MongoDB Java driver via this simple hack (Scala code):

dbo.asInstanceOf[BasicDBObject].copy.asInstanceOf[DBObject]

I'm using it in "Subset" library: https://github.com/osinka/subset2/blob/master/src/main/scala/DBO.scala#L97



 Comments   
Comment by Jeffrey Yemin [ 26/Nov/14 ]

Hi Jon,

True, though we would need a different name than copy. If you'd like to suggest an alternative, please open a new issue for it, as this one is about the new Document class in the 3.0.x branch, not about BasicDBObject.

Thanks,
Jeff

Comment by Jon Gorrono [ 26/Nov/14 ]

Your right, not much you can do... but you can create a new signature and deprecate the old one... it's got a long tail, but it's something.

Comment by Jeffrey Yemin [ 25/Nov/14 ]

There is no good reason that I can see for BasicDBObject.copy to return Object, but since changing it will break binary compatibility we are kind of stuck with it.

Comment by Jon Gorrono [ 24/Nov/14 ]

The comment for copy() is:
/**

  • Creates a new instance which is a copy of this BasicDBObject.
    *
  • @return a BasicDBObject with exactly the same values as this instance.
    */

But it returns an java.lang.Object ... is that something to do with the the Map having Object as a value type, or should I create a ticket?

Comment by Jeffrey Yemin [ 10/Jun/14 ]

In the general case I don't think this can be done properly. Since Document is a Map<String, Object> we can't know for sure that every value in the Map is deep-copyable.

That said, we are working on a new class called BsonDocument that has a closed type system of Map<String, BsonValue>. This class could easily support a deep copy via constructor:

    public BsonDocument(BsonDocument from)

Comment by Ross Lawley [ 09/May/14 ]

https://github.com/mongodb/mongo-java-driver/pull/195

Comment by Adam Brightwell [ 20/Aug/13 ]

From my testing and inspection of the code, it would appear that this method and functionality is already available in both BasicDBObject and BasicDBList as of 7a3a2364. It would appear that this is a "deep copy"?

Generated at Thu Feb 08 08:53:24 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.