如何从Firestore获取没有元数据的文档 [英] How to get documents without metadata from firestore
问题描述
在Firestore中,有 city
, posts
和 users
个集合. posts
有参考一个 user
和一个 city
的文档,我想从所有帖子中检索所有数据并编写查询:
In firestore there are collections cities
, posts
and users
. posts
have documents with reference to one user
and a city
, I wanted to retrieve all data from all posts and wrote a query:
firestore.collection('posts')
.get()
.then(snapshot => {
snapshot.docs.forEach(post => {
console.log(post.data())
});
});
尽管帖子中只有一个文档,但是在控制台中我却获得了数千行数据,我所需要的只是这样:
although I have only one document in posts, in my console I get thousands of lines of data, all I need is something like this:
postId: {
city: "city_name",
user: {
name: "Name",
lastName: "Last"
},
description: "bla bla"
}
如何仅获取所需的数据而没有所有额外的元数据?这是大约30%的输出:
how can I get only needed data without all extra metadata? Here is the about 30% of output:
<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()操作将返回.如果需要较少的数据,则必须将数据拆分为不同的文档,然后分别进行查询.
With a document get(), you can't filter (reduce) the fields in it. When using mobile and web client SDKs, all get() operations return for all fields for all documents matched. If you need less data, you'll have to split the data into different documents and query them individually.
这篇关于如何从Firestore获取没有元数据的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!