Conversion from String to MongoDB ObjectID

前端 未结 5 1490
情深已故
情深已故 2021-01-20 09:18

I tried converting my String ID to MongoDB ObjectID

public class relevancy_test extends  Object implements Comparable {
    public static voi         


        
5条回答
  •  情歌与酒
    2021-01-20 09:58

    As I see there are two issue here:

    1. How to get proper id of ObjectID instance?

    The value 8001_469437317594492928_1400737805000 is not a HEX value which you can see in the DB but an explicit concatenation of time, machine id, pid and counter components. This components are used to generate HEX value. To get HEX value you need to use method ToString of your ObjectID instance.

    Reference to explanation of ObjectID components here: https://api.mongodb.com/java/3.0/org/bson/types/ObjectId.html

    1. How to create ObjectId instance with specific Id

    In order to create new ObjectID instance with specific HEX value use this: var objectId = new ObjectId(hexStringId)

提交回复
热议问题