简体   繁体   中英

How to convert array string of MongoDB json to bson. C# Mongo Bson?

I have a file. The file has the following text:

[
{
    _id: ObjectId("5da08d49949b4c000100b90b"),
    ModifiedOn: ISODate("2019-10-11T14:10:17.461Z"),
    DateOfCreation: ISODate("2019-10-11T14:10:17.459Z"),
    DateModified: ISODate("2019-10-11T14:10:17.459Z"),
    Region: null,
    UniqueNumber: Long("465561"),
    Numiration: 1,
    Code: '001Е',
    User: { ID: ObjectId("someid") },
  }
  ]

I tried to do the following.

JsonConvert.DeserializeObject(text);

But it's not working. It will throw exception.

Unexpected character encountered while parsing value: O. Path '[0]._id', line 3, position 9.

What i can do? I wanna convert this text to bsonarray. Without custom class.

I also tried to do that:

BsonArray array = BsonSerializer.Deserialize<BsonArray>(str);

but it throw exception JSON reader was expecting a value but found 'Long'.

ObjectId(...) , Long(...) , and ISODate(...) are not valid JSON.

If you have the object in the mongo shell, you might try using the tostrictjson built-in function to convert it, but note that the mongo shell will want you to use NumberLong() instead of Long()

In strict JSON, those values would look something like"

{ 
    "_id" : { "$oid" : "5da08d49949b4c000100b90b" }, 
    "ModifiedOn" : { "$date" : "2019-10-11T07:10:17.461-0700" }, 
    "UniqueNumber" : { "$numberLong" : "465561" } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM