在Firestore中,有集合cities
,posts
和users
。 posts
的文档引用了一个user
和一个city
,我想从所有帖子中检索所有数据并编写查询:
firestore.collection('posts')
.get()
.then(snapshot => {
snapshot.docs.forEach(post => {
console.log(post.data())
});
});
尽管帖子中只有一个文档,但是在控制台中我却获得了数千行数据,我所需要的只是这样:
postId: {
city: "city_name",
user: {
name: "Name",
lastName: "Last"
},
description: "bla bla"
}
如何仅获取所需的数据而没有所有额外的元数据? 这是大约30%的输出:
<code>
},
"persistence": [Circular],
"queries": ObjectMap {
"inner": Object {},
"mapKeyFn": [Function anonymous],
},
"references": ReferenceSet {
"refsByKey": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
},
"refsByTarget": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
},
},
"targetCount": 0,
"targetIdGenerator": TargetIdGenerator {
"generatorId": 0,
"nextId": 4,
},
},
"referenceDelegate": MemoryEagerDelegate {
"inMemoryPins": ReferenceSet {
"refsByKey": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
},
"refsByTarget": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
},
},
"orphanedDocuments": Set {},
"persistence": [Circular],
},
"remoteDocumentCache": MemoryRemoteDocumentCache {
"docs": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
"indexManager": MemoryIndexManager {
"collectionParentIndex": MemoryCollectionParentIndex {
"index": Object {
"species": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBNode {
"color": false,
"key": ResourcePath {
"len": 0,
"offset": 5,
"segments": Array [
"projects",
"dallol",
"databases",
"(default)",
"documents",
"species",
"sigir",
],
},
"left": LLRBEmptyNode {
"size": 0,
},
"right": LLRBEmptyNode {
"size": 0,
},
"size": 1,
"value": true,
},
},
},
},
},
},
"newDocumentChanges": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBNode {
"color": false,
"key": DocumentKey {
"path": ResourcePath {
"len": 2,
"offset": 5,
"segments": Array [
"projects",
"dallol",
"databases",
"(default)",
"documents",
"species",
"sigir",
],
},
},
"left": LLRBEmptyNode {
"size": 0,
},
"right": LLRBEmptyNode {
"size": 0,
},
"size": 1,
"value": true,
},
},
},
"size": 0,
"sizer": [Function sizer],
},
},
"queryCache": MemoryQueryCache {
"highestSequenceNumber": 7,
"highestTargetId": 2,
"lastRemoteSnapshotVersion": SnapshotVersion {
"timestamp": Timestamp {
"nanoseconds": 715146000,
"seconds": 1564841214,
},
},
"persistence": MemoryPersistence {
"_started": true,
"clientId": "Qu1aNCEhg2urHD6JrfxZ",
"indexManager": MemoryIndexManager {
"collectionParentIndex": MemoryCollectionParentIndex {
"index": Object {
"species": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBNode {
"color": false,
"key": ResourcePath {
"len": 0,
"offset": 5,
"segments": Array [
"projects",
"dallol",
"databases",
"(default)",
"documents",
"species",
"sigir",
],
},
"left": LLRBEmptyNode {
"size": 0,
},
"right": LLRBEmptyNode {
"size": 0,
},
"size": 1,
"value": true,
},
},
},
},
},
},
"listenSequence": ListenSequence {
"previousValue": 9,
},
"mutationQueues": Object {
"uid:TS2JIdZl7nRD524TApPmiVFzV1l2": MemoryMutationQueue {
"batchesByDocumentKey": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
},
"indexManager": MemoryIndexManager {
"collectionParentIndex": MemoryCollectionParentIndex {
"index": Object {
"species": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBNode {
"color": false,
"key": ResourcePath {
"len": 0,
"offset": 5,
"segments": Array [
"projects",
"dallol",
"databases",
"(default)",
"documents",
"species",
"sigir",
],
},
"left": LLRBEmptyNode {
"size": 0,
},
"right": LLRBEmptyNode {
"size": 0,
},
"size": 1,
"value": true,
},
},
},
},
},
},
"lastStreamToken": "",
"mutationQueue": Array [],
"nextBatchId": 1,
"referenceDelegate": MemoryEagerDelegate {
"inMemoryPins": ReferenceSet {
"refsByKey": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
},
"refsByTarget": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
},
},
"orphanedDocuments": Set {},
"persistence": [Circular],
},
},
},
"queryCache": [Circular],
"referenceDelegate": MemoryEagerDelegate {
"inMemoryPins": ReferenceSet {
"refsByKey": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
},
"refsByTarget": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
},
},
"orphanedDocuments": Set {},
"persistence": [Circular],
},
"remoteDocumentCache": MemoryRemoteDocumentCache {
"docs": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
"indexManager": MemoryIndexManager {
"collectionParentIndex": MemoryCollectionParentIndex {
"index": Object {
"species": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBNode {
"color": false,
"key": ResourcePath {
"len": 0,
"offset": 5,
"segments": Array [
"projects",
"dallol",
"databases",
"(default)",
"documents",
"species",
"sigir",
],
},
"left": LLRBEmptyNode {
"size": 0,
},
"right": LLRBEmptyNode {
"size": 0,
},
"size": 1,
"value": true,
},
},
},
},
},
},
"newDocumentChanges": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBNode {
"color": false,
"key": DocumentKey {
"path": ResourcePath {
"len": 2,
"offset": 5,
"segments": Array [
"projects",
"dallol",
"databases",
"(default)",
"documents",
"species",
"sigir",
],
},
},
"left": LLRBEmptyNode {
"size": 0,
},
"right": LLRBEmptyNode {
"size": 0,
},
"size": 1,
"value": true,
},
},
},
"size": 0,
"sizer": [Function sizer],
},
},
"queries": ObjectMap {
"inner": Object {},
"mapKeyFn": [Function anonymous],
},
"references": ReferenceSet {
"refsByKey": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
},
"refsByTarget": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
},
},
"targetCount": 0,
"targetIdGenerator": TargetIdGenerator {
"generatorId": 0,
"nextId": 4,
},
},
"queryDataByTarget": Object {},
"remoteDocuments": MemoryRemoteDocumentCache {
"docs": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
"indexManager": MemoryIndexManager {
"collectionParentIndex": MemoryCollectionParentIndex {
"index": Object {
"species": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBNode {
"color": false,
"key": ResourcePath {
"len": 0,
"offset": 5,
"segments": Array [
"projects",
"dallol",
"databases",
"(default)",
"documents",
"species",
"sigir",
],
},
"left": LLRBEmptyNode {
"size": 0,
},
"right": LLRBEmptyNode {
"size": 0,
},
"size": 1,
"value": true,
},
},
},
},
},
},
"newDocumentChanges": SortedSet {
"comparator": [Function anonymous],
"data": SortedMap {
"comparator": [Function anonymous],
"root": LLRBNode {
"color": false,
"key": DocumentKey {
"path": ResourcePath {
"len": 2,
"offset": 5,
"segments": Array [
"projects",
"dallol",
"databases",
"(default)",
"documents",
"species",
"sigir",
],
},
},
"left": LLRBEmptyNode {
"size": 0,
},
"right": LLRBEmptyNode {
"size": 0,
},
"size": 1,
"value": true,
},
},
},
"size": 0,
"sizer": [Function sizer],
},
},
"networkEnabled": true,
"onlineStateTracker": OnlineStateTracker {
"asyncQueue": AsyncQueue {
"delayedOperations": Array [],
"operationInProgress": true,
"tail": Promise {
"_40": 0,
"_55": Promise {
"_40": 0,
"_55": null,
"_65": 0,
"_72": null,
},
"_65": 3,
"_72": null,
},
},
"onlineStateHandler": [Function remoteStoreOnlineStateChangedHandler],
"onlineStateTimer": null,
"shouldWarnClientIsOffline": false,
"state": 1,
"watchStreamFailures": 0,
},
"syncEngine": [Circular],
"watchChangeAggregator": WatchChangeAggregator {
"metadataProvider": [Circular],
"pendingDocumentTargetMapping": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
"pendingDocumentUpdates": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
"pendingTargetResets": SortedSet {
"comparator": [Function primitiveComparator],
"data": SortedMap {
"comparator": [Function primitiveComparator],
"root": LLRBEmptyNode {
"size": 0,
},
},
},
"targetStates": Object {
"2": TargetState {
"_current": true,
"_hasPendingChanges": false,
"_resumeToken": "CgkIisru5e/m4wI=",
"documentChanges": SortedMap {
"comparator": [Function anonymous],
"root": LLRBEmptyNode {
"size": 0,
},
},
"pendingResponses": 0,
},
},
},
"watchStream": PersistentListenStream {
"backoff": ExponentialBackoff {
"backoffFactor": 1.5,
"currentBaseMs": 0,
"initialDelayMs": 1000,
"lastAttemptTime": 1564841257133,
"maxDelayMs": 60000,
"queue": AsyncQueue {
"delayedOperations": Array [],
"operationInProgress": true,
"tail": Promise {
"_40": 0,
"_55": Promise {
"_40": 0,
"_55": null,
"_65": 0,
"_72": null,
},
"_65": 3,
"_72": null,
},
},
"timerId": "listen_stream_connection_backoff",
"timerPromise": null,
},
"closeCount": 0,
"connection": WebChannelConnection {
"baseUrl": "https://firestore.googleapis.com",
"databaseId": DatabaseId {
"database": "(default)",
"projectId": "dallol",
},
"forceLongPolling": false,
},
"credentialsProvider": FirebaseCredentialsProvider {
"app": FirebaseAppImpl {
"INTERNAL": Object {
"addAuthTokenListener": [Function bound ],
"getToken": [Function bound ],
"getUid": [Function bound ],
"removeAuthTokenListener": [Function bound ],
},
"automaticDataCollectionEnabled_": false,
"firebase_": Object {
"INTERNAL": Object {
"ErrorFactory": [Function ErrorFactory],
"createFirebaseNamespace": [Function createFirebaseNamespace],
"createSubscribe": [Function createSubscribe],
"deepExtend": [Function deepExtend],
"extendNamespace": [Function extendNamespace],
"factories": Object {
"auth": [Function anonymous],
"database": [Function anonymous],
"firestore": [Function anonymous],
"storage": [Function factory],
},
},
},
}
</code>
答案 0 :(得分:0)
使用文档get(),您无法过滤(减少)其中的字段。使用移动和Web客户端SDK时,对于所有匹配的文档,所有字段的 all get()操作将返回。如果您需要较少的数据,则必须将数据拆分为不同的文档,然后分别进行查询。