javascript - 如何从 firestore 获取没有元数据的文档

标签 javascript reactjs google-cloud-firestore

在firestore中有集合citiespostsusersposts 包含引用一个 usercity 的文档,我想检索所有帖子中的所有数据并编写一个查询:

 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>

最佳答案

使用文档 get(),您无法过滤(减少)其中的字段。使用移动和 Web 客户端 SDK 时,all get() 操作会返回所有匹配文档的 all 字段。如果您需要较少的数据,则必须将数据拆分到不同的文档中并单独查询它们。

关于javascript - 如何从 firestore 获取没有元数据的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57335738/

相关文章:

javascript - 商店更改时更新组件

javascript - 从dom元素获取href属性

javascript - json循环后推送2个数组

Javascript 按键时间

reactjs - Polaris React App Bridge 文档中的 OAuth 示例有什么用?

javascript - 将 Firebase 模拟器与 AngularFire 一起使用

android - Firestore 数据库 : Updating missing documents

firebase - 是否需要更改软件包名称?

javascript - 获取点击的 ul 的 li 列表

javascript - javascript .split (' ' ) 会将逗号留在数组中吗?