Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert document via MongoClient, the value of AnyBSON(Date) will be transferred to Timestamp @ v10.0.0-beta.2 #6654

Closed
ananfang opened this issue Jul 17, 2020 · 6 comments
Assignees

Comments

@ananfang
Copy link

ananfang commented Jul 17, 2020

Goals

Using the app.mongoClient to insert the document, the value of AnyBSON(Date) will be transferred to Timestamp in MongoDB, and even the timestamp is wrong.

Expected Results

It should be recorded as the Date type in the MongoDB.

Actual Results

let app = RealmApp(id: "realm-app-id")
let collection = app.mongoClient("mongodb-atlas").database(withName: "some-database").collection(withName: "some-collection")
let document: Document = [
  "updatedAt": AnyBSON(Date(timeIntervalSince1970: 0))
]
collection.insertOne(document) { objectId, error in
  // Do something after inserting
}

The field "updatedAt" will be the type of Timestamp, and the value will be always different.
(e.g. 6850268160453509127, 6850268302187429898 and etc., extremely big and obviously not timestamp number)

Steps to Reproduce

Check Actual Results

Code Sample

Check Actual Results

Version of Realm and Tooling

Realm framework version: v10.0.0-beta.2

Realm Object Server version: MongoDB Realm

Xcode version: 11.5

iOS/OSX version: 13.5 (simulator)

Dependency manager + version: CocoaPods 1.9.3

@homathko
Copy link

Same issue. Passing Foundation.Date object into AnyBSON.datetime( ) stores the document in Atlas with a timestamp value, which cannot be read by the realm client -> "Object was deleted or invalidated". I've spent about 5 hours now trying to sort this out and can't find any documentation. This is the closest hit yet and it was posted almost 5 months ago...?

@leemaguire
Copy link
Contributor

@ananfang this is the expected behaviour, as the BSON speciation states:

BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970). This results in a representable date range of about 290 million years into the past and future.

https://docs.mongodb.com/manual/reference/bson-types/#date

@leemaguire
Copy link
Contributor

@homathko can you share your MongoDB Realm schema & Swift object with us so we can investigate?

@homathko
Copy link

I wish I could! This was awhile ago and is out of my scope now - I am having trouble recalling any details. It was working with cloud functions and custom user data, so this probably isn't the place for further troubleshooting anyhow..

@ananfang
Copy link
Author

@ananfang this is the expected behaviour, as the BSON speciation states:

BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970). This results in a representable date range of about 290 million years into the past and future.

https://docs.mongodb.com/manual/reference/bson-types/#date

@leemaguire
If you are using the Realm database to record the Date field, it will be also Date field in the MongoDB side.
This will be inconsistent between MongoDB Access and Realm.

@leemaguire
Copy link
Contributor

Fixed by #7062

@sync-by-unito sync-by-unito bot closed this as completed Jan 23, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants