Javascript 查询firestore数据库中的文档id

Javascript 查询firestore数据库中的文档id,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,我想查询firestore数据库中的文档id。目前我有以下代码: db.collection('books').where('id', '==', 'fK3ddutEpD2qQqRMXNW5').get() 我没有结果。但当我查询其他字段时,它会起作用: db.collection('books').where('genre', '==', 'biography').get() 文档id的名称如何命名?试试以下方法: db.collection('books').doc('fK3ddutEp

我想查询firestore数据库中的文档id。目前我有以下代码:

db.collection('books').where('id', '==', 'fK3ddutEpD2qQqRMXNW5').get()
我没有结果。但当我查询其他字段时,它会起作用:

db.collection('books').where('genre', '==', 'biography').get()
文档id的名称如何命名?

试试以下方法:

db.collection('books').doc('fK3ddutEpD2qQqRMXNW5').get()

(第一个查询是查找一个名为“id”的显式用户集字段,这可能不是您想要的。)

您可以按照以下模式通过其
id
获取文档:

firebase
.firestore()
.收藏(“您的收藏”)
.doc(“文档ID”)
.get()
.then((docRef)=>{console.log(docRef.data())}
.catch((错误)=>{})

我有点晚了,但实际上有办法做到这一点

db.collection('books').where(firebase.firestore.FieldPath.documentId(), '==', 'fK3ddutEpD2qQqRMXNW5').get()
当您处理firebase安全规则,并且只想查询允许访问的记录时,这可能很有用。

从中获取文档

var docRef = db.collection("cities").doc("SF");

docRef.get().then(function(doc) {
    if (doc.exists) {
        console.log("Document data:", doc.data());
    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
}).catch(function(error) {
    console.log("Error getting document:", error);
});

您可以使用
\uuuu name\uuuu
关键字在查询中使用文档ID

而不是这个
db.collection('books').doc('fk3ddutepd2qqqrmmxnw5')。get()
您可以编写

db.collection('books')。其中('uuuu name'、'='、'fk3ddutepd2qqqrmmxnw5')。get()

在这种情况下,应该返回一个长度为
1
的数组


firebase文档在规则文档中提到了此功能

只是为了澄清这里的混乱

记住,无论是获取完整的
集合
还是单个
文档
,都应该使用
async/wait
来获取数据

异步函数someFunction(){
等待db.collection('books').doc('fK3ddutEpD2qQqRMXNW5').get();
}

2021年6月

新的是树摇动和结果在较小的编译应用程序。建议所有新的Firestore应用程序都使用它

从“firebase/firestore”导入{doc,getDoc};
const snap=wait getDoc(doc(db,'books','fk3ddutepd2qqqqrmmxnw5'))
if(snap.exists()){
console.log(snap.data())
}
否则{
console.log(“无此类文档”)
}
这是基于

从“firebase/firestore”导入{doc,getDoc};
const docRef=doc(db,“cities”,“SF”);
const docSnap=wait getDoc(docRef);
if(docSnap.exists()){
log(“文档数据:”,docSnap.data());
}
否则{
//在这种情况下,将不定义doc.data()
log(“没有这样的文档!”);
}
你可以把它变成一个助手函数

异步函数getDocument(coll,id){ const snap=wait getDoc(doc(db,coll,id)) if(snap.exists()) 返回snap.data() 其他的 return Promise.reject(错误(`没有这样的文档:${coll}.${id}')) } getDocument(“书籍”,“FK3DDUTEPD2QQRMXNW5”)
嘿,你知道如何获取符合FireStore查询中提到的某些条件的文档ID吗?如果我们需要多个where case(多个ID),该怎么办?在python中,我获取了
AttributeError:'CollectionReference'对象没有属性'doc'。
。原来语法是
db.collection('books').document('longid').get()
@Midhilaj:可能是文档id是唯一的吗?@Midhilaj在我的例子中,我有另一个集合,其中我有每个用户的文档,存储满足特定条件的数据。我有非常有限的条件,例如哪个用户上传了哪本书。你能说明什么是firebase.firestore.FieldPath是从何处初始化的吗?如果其他任何人正在努力找到这个导入,请从“firebase”导入它的“import{firestore};”“完美”。我将它用于
.where(firebase.firestore.FieldPath.documentId(),“in”,引用列表)
。这太棒了。你在哪里看到的?在flatter中:firestore.collection(“collection_name”).Where(FieldPath.documentId,其中:mList).getDocuments();当在prod或emulator中使用Firestore UI时,这很方便,在这里你不能使用类似
db.collection('foo').doc('xxxx')
这是我在云函数实现中需要的,客户端发送文档ID,我需要用“in”查询所有文档并获得docsThanks mate,这就是我要寻找的信息@divyansunegi管理员SDK有一个
getAll()
函数,我想这就是你想要的。对吗?