I am trying to run an Atlas query via Realm using swift, code is as follows:
app.login(credentials: Credentials.anonymous) { results in
switch results {
case .failure(let error):
print("Login failed: \(error.localizedDescription)")
case .success(let user):
print("Successfully logged in as user \(user)")
let client = self.app.currentUser!.mongoClient("mongodb-atlas")
let database = client.database(named: "Incidents")
let collection = database.collection(withName: "Incidents")
let qf: Document = ["incidentDetails.lastUpdated":"{$gt:ISODate('2022-07-23')}"]
collection.find(filter: qf) { result in
switch result {
case .failure(let error):
print("Login failed: \(error.localizedDescription)")
case .success(let documents):
print("Great Success")
for doc in documents {
print("Title: \(doc)")
}
}
}
}
}
This executes successfully, logs an anon. user in runs the query returning success, however I get no documents back. I assume at this point it is because of my filter document
let qf: Document = ["incidentDetails.lastUpdated":"{$gt:ISODate('2022-07-23')}"]
But I am at a loss as to how to write this in Swift. Essentially, I only want to return documents updated within the last few days. I can run this via Atlas using
Some basic troubleshooting may help clarify the issue.
I assume the object you are querying for has other properties. Try to just read that object via another property with a known value and see if you get a result. If not, there’s a setup or config issue. If so, then the query in the question is malformed.
Try something like this
let qf: Document = ["some_property": "some_known_value"]
collection.findOneDocument(filter: qf) { result in
Also… isn’t the Date done line this new Date("<YYYY-mm-dd>") which returns an ISODate or use the date format ISODate("2022-07-23T 09:10:24.000Z"