简体   繁体   中英

How can get only want object value from firestore by react native

I try to get only object value that I want but whenever I query with array-contains in where(),come out all data value from that collection.

getid Array [
  Object {
    "timestamp": Object {
      "nanoseconds": 738000000,
      "seconds": 1639994269,
    },
    "userMatch": Array [
      "6Lf5bHwzCSbI2bmoMv7KuduMGwe2",
      "E8R52y6dD8XI3shXhnhS9pVzUpe2",
    ],
    "users": Object {
      "6Lf5bHwzCSbI2bmoMv7KuduMGwe2": Object {
        "about": "gshjs",
        "age": "25",
        "displayName": "ry",
        "gender": "female",
        "id": "6Lf5bHwzCSbI2bmoMv7KuduMGwe2",
        "interestIn": "male",
        "job": "fh",
        "photo": "https://firebasestorage.googleapis.com/v0/b/match-me-now.appspot.com/o/images%2F6Lf5bHwzCSbI2bmoMv7KuduMGwe2%2FprofilePicture.jpeg?alt=media&token=e1b8d794-4074-4e10-93f1-e2f5c9835fa2",
        "timestamp": Object {
          "nanoseconds": 44000000,
          "seconds": 1639994173,
        },
      },
      "E8R52y6dD8XI3shXhnhS9pVzUpe2": Object {
        "about": "fdhnsn",
        "age": "25",
        "displayName": "gsj",
        "gender": "male",
        "id": "E8R52y6dD8XI3shXhnhS9pVzUpe2",
        "interestIn": "female",
        "job": "hsh",
        "photo": "https://firebasestorage.googleapis.com/v0/b/match-me-now.appspot.com/o/images%2FE8R52y6dD8XI3shXhnhS9pVzUpe2%2FprofilePicture.jpeg?alt=media&token=b93935cf-8285-4059-8495-2243fdb89fff",
        "timestamp": Object {
          "nanoseconds": 201000000,
          "seconds": 1639994235,
        },
      },
    },
  },
]

I only want is userMatch (E8R52y6dD8XI3shXhnhS9pVzUpe2) id value

"userMatch": Array [
      "6Lf5bHwzCSbI2bmoMv7KuduMGwe2",
      "E8R52y6dD8XI3shXhnhS9pVzUpe2",
    ],

I fetch as follow

  
    useEffect(async()=>{
      const matchid = query(collection(db,'matches'),where('userMatch','array-contains',user.uid))
      const getid = await getDocs(matchid).then((snapshot)=>snapshot.docs.map((doc)=>doc.data()))
       
      console.log('getid',getid)


    },[db])

why not get only user.uid in userMatch object. although where() method filter 'userMatch' object field, why get all object field in matches collection.how can get only userMatch object value. plz help

Firestore client-side SDKs always returns entire documents. They have no way to retrieve only some fields of a document.

A query (as you use here with array-contains can be used to limit which documents are returns from the database, but not what field are returned.


If you commonly find that you only need some fields from a larger document, consider storing those fields in a separate document. While you'll be duplicating data, you'll have less data to read when needed.

In your use-case you could also consider storing the userMatches in a subcollection for each user document, and then using a collection group query to find the relevant documents.


Also see:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM