하위 컬렉션을 통한 클라우드 Firestore 심층 분석
'todos'라는 이름의 루트 컬렉션이 있다고 가정해 보겠습니다.
이 컬렉션의 모든 문서에는 다음이 있습니다.
title
끈- 이름이 지정된 하위 컬렉션
todo_items
하위 컬렉션의 모든 문서todo_items
가지다
title
끈completed
부울
Cloud Firestore에서 쿼리하는 것은 기본적으로 얕다는 것을 알고 있습니다. 이는 훌륭하지만, 다음을 쿼리하는 방법이 있습니까?todos
하위 컬렉션이 포함된 결과를 가져옵니다.todo_items
자동으로?
다시 말해서, 다음 쿼리를 어떻게 하면 포함시킬 수 있습니까?todo_items
서브컬렉션?
db.collection('todos').onSnapshot((snapshot) => {
snapshot.docChanges.forEach((change) => {
// ...
});
});
이 유형의 쿼리는 지원되지 않지만 나중에 고려할 수도 있습니다.
파이어스토어에서 딥 쿼리를 수행하는 방법에 관심이 있는 사람이 있다면, 여기 'to_items' 하위 컬렉션이 있는 모든 'todos'를 반환하는 클라우드 함수 getAllTodos 버전이 있습니다.
exports.getAllTodos = function (req, res) {
getTodos().
then((todos) => {
console.log("All Todos " + todos) // All Todos with its todo_items sub collection.
return res.json(todos);
})
.catch((err) => {
console.log('Error getting documents', err);
return res.status(500).json({ message: "Error getting the all Todos" + err });
});
}
function getTodos(){
var todosRef = db.collection('todos');
return todosRef.get()
.then((snapshot) => {
let todos = [];
return Promise.all(
snapshot.docs.map(doc => {
let todo = {};
todo.id = doc.id;
todo.todo = doc.data(); // will have 'todo.title'
var todoItemsPromise = getTodoItemsById(todo.id);
return todoItemsPromise.then((todoItems) => {
todo.todo_items = todoItems;
todos.push(todo);
return todos;
})
})
)
.then(todos => {
return todos.length > 0 ? todos[todos.length - 1] : [];
})
})
}
function getTodoItemsById(id){
var todoItemsRef = db.collection('todos').doc(id).collection('todo_items');
let todo_items = [];
return todoItemsRef.get()
.then(snapshot => {
snapshot.forEach(item => {
let todo_item = {};
todo_item.id = item.id;
todo_item.todo_item = item.data(); // will have 'todo_item.title' and 'todo_item.completed'
todo_items.push(todo_item);
})
return todo_items;
})
}
저도 같은 문제에 직면했지만 IOS에서는 어쨌든 당신의 질문을 받고 작업관리 문서에 자동 ID를 사용한다면 제 경우 제목 필드가 있는 필드로 문서 ID를 저장하는 것이 쉬울 것입니다.
let ref = self.db.collection("collectionName").document()
let data = ["docID": ref.documentID,"title" :"some title"]
검색할 때 할 일의 배열을 말하고 항목을 클릭하면 경로를 따라 쉽게 탐색할 수 있습니다.
ref = db.collection("docID/\(todo_items)")
정확한 코드를 알려드리고 싶지만 자바스크립트에 익숙하지 않습니다.
Angular Firestore(afs) 및 Typescript를 사용했습니다.
import { map, flatMap } from 'rxjs/operators';
import { combineLatest } from 'rxjs';
interface DocWithId {
id: string;
}
convertSnapshots<T>(snaps) {
return <T[]>snaps.map(snap => {
return {
id: snap.payload.doc.id,
...snap.payload.doc.data()
};
});
}
getDocumentsWithSubcollection<T extends DocWithId>(
collection: string,
subCollection: string
) {
return this.afs
.collection(collection)
.snapshotChanges()
.pipe(
map(this.convertSnapshots),
map((documents: T[]) =>
documents.map(document => {
return this.afs
.collection(`${collection}/${document.id}/${subCollection}`)
.snapshotChanges()
.pipe(
map(this.convertSnapshots),
map(subdocuments =>
Object.assign(document, { [subCollection]: subdocuments })
)
);
})
),
flatMap(combined => combineLatest(combined))
);
}
다른 답변에서 지적한 바와 같이, 심층 질의를 요청할 수 없습니다.
추천합니다.가능한 한 데이터를 최소한으로 복제합니다.
저는 "애완동물 소유"와 같은 문제에 직면하고 있습니다.검색 결과에서 사용자가 소유한 각 애완동물을 표시해야 하지만 애완동물을 스스로 검색할 수 있어야 합니다.저는 결국 데이터를 복제했습니다.각 사용자에 대한 애완동물 배열 속성과 애완동물 하위 컬렉션을 가질 예정입니다.저는 이것이 우리가 이런 종류의 시나리오로 할 수 있는 최선이라고 생각합니다.
서류에 따르면 소방서에 전화를 두 번 해야 합니다.필요한 것doc
그리고 두 번째로 가져오는 것.subcollection
전체 시간을 줄이기 위해 할 수 있는 최선의 방법은 다음을 사용하여 이 두 가지 통화를 병렬로 하는 것입니다.promise.All
또는promise.allSettled
순차적인 것이 아니라.
다음과 같은 방법을 사용할 수 있습니다.
db.collection('coll').doc('doc').collection('subcoll').doc('subdoc')
언급URL : https://stackoverflow.com/questions/46611279/cloud-firestore-deep-get-with-subcollection
'programing' 카테고리의 다른 글
눈금 레이블에 대한 부동 소수점 형식 지정 (0) | 2023.07.22 |
---|---|
getApplication()을 컨텍스트로 사용하여 "창을 추가할 수 없음 - 응용 프로그램에 대한 토큰 null이 아닙니다"를 던지는 대화 상자 (0) | 2023.07.17 |
Oracle 10g에서 왼쪽 조인으로 삭제 (0) | 2023.07.17 |
ClassNotFoundException: org.flywaydb.core.api.callback입니다.플라이웨이 콜백 (0) | 2023.07.17 |
사전 값을 여러 개 가져오는 방법? (0) | 2023.07.17 |