Saviz
Saviz

Reputation: 687

In Cloud Firestore how can I add a document with an explicit documentid

Is there a way to add a document to the Cloud Store but being able to set its documentid. The reason I'd like to do that is because I'd like to have the following structure:

users/{user}/userdata

And would like to use userId from the authenticated user in the place of the user's documentId. The examples always show an auto generated DocumentID for the created document. Can't find any documentation that lets me know how to explicitly set the DocumentId.

__block FIRDocumentReference *ref =
[[self.db collectionWithPath:@"users"] addDocumentWithData:@{
  @"first": @"Ada",
  @"last": @"Lovelace",
  @"born": @1815
} completion:^(NSError * _Nullable error) {
  if (error != nil) {
    NSLog(@"Error adding document: %@", error);
  } else {
    NSLog(@"Document added with ID: %@", ref.documentID);
  }
}];

Upvotes: 0

Views: 218

Answers (1)

Saviz
Saviz

Reputation: 687

Figured it out ... hopefully this will help someone else:

[[[self.db collectionWithPath:@"users"] documentWithPath:user.uid] setData:@{
        @"name": @"Jane Smith",
        @"State": @"CA",
        @"country": @"USA"
      } completion:^(NSError * _Nullable error) {
        if (error != nil) {
            NSLog(@"Error writing document: %@", error);
          } else {
            NSLog(@"Document successfully written!");
          }
      }];

Upvotes: 1

Related Questions