13

How do I go about adding the document ID of a document I just added to my firestore database, to said document?

I want to do this so that when a user retrieves a "ride" object and chooses to book it, I can know which specific ride they've booked.

The problem that i'm facing is that you can't get the document ID until after it's created, so the only way to add it to said document would be to create a document, read its ID, then edit the document to add in the ID. At scale this would create twice as many server calls as desired.

Is there a standard way to do this? Or a simple solution to know which "ride" the user booked and edit it accordingly in the database?

struct Ride {
    var availableSeats: Int
    var carType: String
    var dateCreated: Timestamp
    var ID: String // How do I implement this?
}


func createRide(ride: Ride, completion: @escaping(_ rideID: String?, _ error: Error?) -> Void) {
    // Firebase setup
    settings.areTimestampsInSnapshotsEnabled = true
    db.settings = settings

    // Add a new document with a generated ID
    var ref: DocumentReference? = nil
    ref = db.collection("rides").addDocument(data: [
        "availableSeats": ride.availableSeats,
        "carType": ride.carType,
        "dateCreated": ride.dateCreated,
        "ID": ride.ID,
    ]) { err in
        if let err = err {
            print("Error adding ride: \(err)")
            completion(nil, err)
        } else {
            print("Ride added with ID: \(ref!.documentID)")
            completion(ref?.documentID, nil)
            // I'd currently have to use this `ref?.documentID` and edit this document immediately after creating. 2 calls to the database.
        }
    }
}

3 Answers 3

15

While there is a perfectly fine answer, FireStore has the functionality you need built in, and it doesn't require two calls to the database. In fact, it doesn't require any calls to the database.

Here's an example

    let testRef = self.db.collection("test_node")
    let someData = [
        "child_key": "child_value"
    ]

    let aDoc = testRef.document() //this creates a document with a documentID
    print(aDoc.documentID) //prints the documentID, no database interaction
    //you could add the documentID to an object etc at this point
    aDoc.setData(someData) //stores the data at that documentID

See the documentation Add a Document for more info.

In some cases, it can be useful to create a document reference with an auto-generated ID, then use the reference later. For this use case, you can call doc():

You may want to consider a slightly different approach. You can obtain the document ID in the closure following the write as well. So let's give you a cool Ride (class)

class RideClass {
    var availableSeats: Int
    var carType: String
    var dateCreated: String
    var ID: String

    init(seats: Int, car: String, createdDate: String) {
        self.availableSeats = seats
        self.carType = car
        self.dateCreated = createdDate
        self.ID = ""
    }

    func getRideDict() -> [String: Any] {
        let dict:[String: Any] = [
            "availableSeats": self.availableSeats,
            "carType": self.carType,
            "dateCreated": self.dateCreated
        ]
        return dict
    }
}

and then some code to create a ride, write it out and leverage it's auto-created documentID

    var aRide = RideClass(seats: 3, car: "Lincoln", createdDate: "20190122")

    var ref: DocumentReference? = nil
    ref = db.collection("rides").addDocument(data: aRide.getRideDict() ) { err in
        if let err = err {
            print("Error adding document: \(err)")
        } else {
            aRide.ID = ref!.documentID
            print(aRide.ID) //now you can work with the ride and know it's ID
        }
    }
2
  • It seems like double read and write. Free tire would be heavily pounded by this call. Is there any workaround ? Dec 17, 2020 at 7:27
  • @EngineSense Not sure what you mean. In the first part of my answer there's no read OR write at all. In the second example it's a single write. What is Free tire and why would it be heavily pounded since it's a single write?
    – Jay
    Dec 17, 2020 at 19:56
4

I believe that if you use Swift's inbuilt ID generator, called UUID, provided by the Foundation Framework, this will let you do what you want to do. Please see the below code for my recommended changes. Also by doing it this way, when you first initialise your "Ride" struct, you can generate its ID variable then, instead of doing it inside the function. This is the way I generate unique ID's throughout my applications and it works perfectly! Hope this helps!

struct Ride {
    var availableSeats: Int
    var carType: String
    var dateCreated: Timestamp
    var ID: String
}


func createRide(ride: Ride, completion: @escaping(_ rideID: String, _ error: Error?) -> Void) {
    // Firebase setup
    settings.areTimestampsInSnapshotsEnabled = true
    db.settings = settings

    // Add a new document with a generated ID
    var ref: DocumentReference? = nil
    let newDocumentID = UUID().uuidString
    ref = db.collection("rides").document(newDocumentID).setData([
        "availableSeats": ride.availableSeats,
        "carType": ride.carType,
        "dateCreated": ride.dateCreated,
        "ID": newDocumentID,
    ], merge: true) { err in
        if let err = err {
            print("Error adding ride: \(err)")
            completion(nil, err)
        } else {
            print("Ride added with ID: \(newDocumentID)")
            completion(newDocumentID, nil)
        }
    }
}
2
  • Perfect solution! Will this UUID function ever create the same id twice though?
    – yambo
    Jan 22, 2019 at 15:02
  • 2
    No worries! Nope, as long as you do re-use "let newDocumentID = UUID().uuidString" in the same function for something else, say for a new document reference, you should be absolutely fine! 👌
    – WH48
    Jan 22, 2019 at 15:07
2

This is my solution which works like a charm

    let opportunityCollection = db.collection("opportunities")
    let opportunityDocument = opportunityCollection.document()
    let id = opportunityDocument.documentID

    let data: [String: Any] = ["id": id,
                               "name": "Kelvin"]

    opportunityDocument.setData(data) { (error) in
        if let error = error {
            completion(.failure(error))
        } else {
            completion(.success(()))
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.