我对Firestore相当陌生。
我有一个集合,在该集合中有一个文档,其中有几个映射的字段。结构是
Questions
balances
balances = (map)
balances = (map)
hint = (map)
titleColumn = (map)
title_1 = (map)
columnTitle = "Hints"
linkTitle = "hints"
title_2 = (map)
columnTitle = "Other Thing"
linkTitle = "other"我可以使用以下方法查询Firestore:
const collectionId = "Questions";
const documentId = "balances"
const snapshot = await firestore.collection(collectionId).doc(documentId).get();我得到了一个有数据的对象。现在我只对linkTitle =“提示”(所以我可以得到columnTitle)的文档感兴趣。现在我可以使用普通的JavaScript来完成这个任务,但是肯定会有一种查询方法,所以只返回我想要的数据?还是我想的太多像一个旧的SQL数据库?
编辑
function App() {
const firestore = firebase.firestore();
const collectionId = "Questions";
const documentId = "balances"
const [authenticated, setAuthenticated] = useState(false);
const [questionTitle, setQuestionTitle] = useState();
// question things
const [questionText, setQuestionText] = useState("");
const [imageUrl, setImageUrl] = useState("");
// answer things
const [answers, setAnswers] = useState([]);
// hint things
const [hintTitle, setHintTitle] = useState("");
const [hintDetails, setHintDetails] = useState([]);
useEffect(() => {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
setAuthenticated(true);
} else {
setAuthenticated(false);
}
});
const query = firestore
.collection(collectionId)
.where('balances.balances.balances.hint.titleColumn.title_1.linkTitle', '==', "hints")
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
//console.log(doc.data());
console.log("Column Title: ", doc.data().balances.balances.balances.hint.titlecolumn.title_1.columnTitle);
});
});
const getFirebase = async () => {
const snapshot = await firestore.collection(collectionId).doc(documentId).get();
const questionData = snapshot.data();
// get and set the question title
setQuestionTitle(questionData.balances.balances.questions.title)
// get and set the question text and image
setQuestionText(questionData.balances.balances.fullquestion.question)
setImageUrl(questionData.balances.balances.fullquestion.imageUrl)
// deal with the answers
// create a new answer array
const answerArr = [];
// add the answer from firestore to the answerArr
Object.keys(questionData.balances.balances.answers).forEach(key => {
answerArr.push(questionData.balances.balances.answers[key]);
});
// set the answers to be the answersArray
setAnswers(answerArr)
// get and set hint title
setHintTitle(query)
// get and set all hints
setHintDetails(questionData.balances.balances.hint)
}
getFirebase();
}, [firestore]); 我正在从useEffect中的Cloudstore检索数据,并使用状态。然而,这种询问似乎是在逃避我。现在还有一件事,这可能是我现在还没有得到它,那就是示例查询依赖于知道我需要查看title_1 --是否存在通配符,以便查询该级别下的任何文档?
发布于 2022-03-04 09:25:19
据我所知,您的问题和您的Firestore数据结构;
我复制了您的Fi还原数据结构,如下所示:

而且,通过使用@Frank在评论中也提到的查询,您可以获得columnTitle,也可以回答您的问题
是示例查询依赖于知道我需要查看title_1是否存在通配符这样就可以查询该级别下的任何文档了吗?
我更新了代码,以便显示titleColumn下的任何titleColumn。
var collection = "Questions";
var document = "balances";
async function getHints(title) {
const query = db
.collection(collection)
.where(`${document}.balances.hint.titleColumn.${title}.linkTitle`, `==`, `hints`)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
var documents = doc.data();
var result = documents.balances.balances.hint.titleColumn[title];
console.log(`${title}:`, result);
});
});
}
var docRef = db.collection(collection).doc(document);
docRef.get().then((doc) => {
if (doc.exists) {
var titles = doc.data().balances.balances.hint.titleColumn;
for (let title in titles) {
getHints(title);
}
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch((error) => {
console.log("Error getting document:", error);
});其结果将是:
title_1: { linkTitle: 'hints', columnTitle: 'Hints' }https://stackoverflow.com/questions/71343260
复制相似问题