Javascript 如何将具有自定义 ID 的文档添加到 Firestore

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48541270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:09:22  来源:igfitidea点击:

How to add Document with Custom ID to firestore

javascriptfirebasegoogle-cloud-firestore

提问by Harvey Dent

Is there any chance to add a document to firestore collection with custom generated id, not the id generated by firestore engine?

是否有机会使用自定义生成的 id 将文档添加到 firestore 集合,而不是 firestore 引擎生成的 id?

回答by Finlay Percy

To use a custom ID you need to use .set, rather than .add

要使用自定义 ID,您需要使用.set,而不是.add

This creates a document with the ID "LA":

这将创建一个 ID 为“LA”的文档:

db.collection("cities").doc("LA").set({
    name: "Los Angeles",
    state: "CA",
    country: "USA"
})

This is taken from the official docs here

这是取自此处的官方文档

回答by Papa Kojo

this.afs.collection('[your collection]').doc('[your ID]').set([your document]);

回答by David Thong Nguyen

To expand on the accepted answer, if you ever wanted your client to generate a random ID for the doc before pushing to Firestore (assuming the same createId()function exists outside of AngularFire2)

为了扩展已接受的答案,如果您希望您的客户在推送到 Firestore 之前为文档生成一个随机 ID(假设在createId()AngularFire2 之外存在相同的功能)

const newId = db.createId();
db.collection("cities").doc(newId).set({
    name: "Los Angeles",
    state: "CA",
    country: "USA"
})

This is useful for setting the ID as a reference field in another document, even before Firestore saves anything. If you don't need to use the saved object right away, this speeds up the process by not making you wait for the ID. The set()call is now asynchronous from the pipe you might be using in Angular

这对于将 ID 设置为另一个文档中的参考字段非常有用,甚至在 Firestore 保存任何内容之前也是如此。如果您不需要立即使用保存的对象,这不会让您等待 ID,从而加快进程。该set()调用现在与您可能在 Angular 中使用的管道异步

Notice I didn't put id: newIdin the set object, because Firestore by default doesn't save ID as a field in the doc

注意我没有放入id: newIdset 对象,因为默认情况下 Firestore 不会将 ID 保存为文档中的字段

回答by Jonel Dominic Brave

You can do it this way

你可以这样做

// Inject AngularFirestore as dependency 
private angularFireStore: AngularFirestore // from from 'angularfire2/firestore'

// set user object to be added into the document
let user = {
  id: this.angularFireStore.createId(),
  name: 'new user'
  ...
}

// Then, finally add the created object to the firebase document
angularFireStore.collection('users').doc(user.id).set(user);

回答by Deven

db.collection("users").document(mAuth.getUid()).set(user)

db.collection("users").document(mAuth.getUid()).set(user)

Here, the name of the collection is "users"and the document name is the user's UID

这里,集合的名称是"users",文档名称是用户的UID

Here u need to use setnot add

在这里你需要使用setnotadd

private void storeData(String name, String email, String phone) {

    // Create a new user with a first and last name
    Map<String, Object> user = new HashMap<>();
    user.put("name", name);
    user.put("email", email);
    user.put("phone", phone);

    // Add a new document with a generated ID
    db.collection("users").document(mAuth.getUid()).set(user)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Toasty.success(context,"Register sucess",Toast.LENGTH_SHORT).show();
        }
    });
}

回答by sun sreng

Create new Document with ID

创建带有 ID 的新文档

  createDocumentWithId<T>(ref: string, document: T, docId: string) {
    return this.afs.collection(ref).doc<T>(docId).set(document);
  }

EX: this example with take email as ID for the document

EX:此示例以电子邮件作为文档的 ID

this.fsService.createDocumentWithId('db/users', {}, credential.user.email);