2017-12-17 14 views
1

Firestore 문서의 배열에 값을 추가하고 목록 또는 배열로 가져 오려고합니다.클라우드 Firestore 문서의 배열 필드에 값을 추가해야합니다.

이것은 내 코드이며, 0 인덱스에서 배열 값을 바꿉니다. 목록을 만들고 부울 값을 반환했습니다. arrays에 관한 offical 한 문서에서 보는 바와 같이

FirebaseFirestore db = FirebaseFirestore.getInstance(); 
final DocumentReference doc = db.collection("RoomInformation").document(model.getRoomAdmin()); 

db.collection("RoomInformation").document(model.getRoomAdmin()).update(freshUser).addOnCompleteListener(new OnCompleteListener<Void>() { 
    @Override 
    public void onComplete(@NonNull Task<Void> task) { 
     if (task.isSuccessful()) { 
      doc.update("onlineUsers" , Arrays.asList("user" , facebookUserName)).addOnSuccessListener(new OnSuccessListener<Void>() { 
       @Override 
       public void onSuccess(Void aVoid) { 
        new OurToast().myToast(view.getContext() , facebookUserName + " Join The Room"); 
       } 
      }); 
     } 
    } 
+0

)를 그리고 난 collections.singltonlist 함께 만든()와 다른 방에 가입하면 변경이 예에서 봐 주시기 바랍니다 이전 사용자 – alserdar

답변

0

: 클라우드 경우 FireStore가 배열을 저장할 수 있지만

,이 배열 구성원을 조회 또는 단일 배열 요소를 업데이트 지원하지 않습니다. 예를 들어 다음과 같은 코드를 촬영

:

public class ArrayPost { 
    String title; 
    String[] categories; 

    public ArrayPost(String title, String[] categories) { 
     this.title = title; 
     this.categories = categories; 
    } 
} 

ArrayPost myArrayPost = new ArrayPost("My great post", new String[]{ 
    "technology", "opinion", "cats" 
}); 

우리는 위의 데이터 구조를 사용하여이 쿼리를 수행 할 수있는 방법이 없음을 참조하십시오.

Cloud Firestore에서 배열을 저장할 수 있지만 한 가지 더주의해야 할 점은 반 패턴입니다. Firebase가 어레이를 사용하지 말 것을 권장하는 많은 이유 중 하나는 보안 규칙을 작성하는 것이 불가능하다는 것입니다. 나는 각 카테고리가지도의 키이고 다른 값이 모두 true 인 대체 데이터 구조를 고려해야한다고 생각합니다. 내가 새로운 사용자를 추가 싶어, 내가 (arrays.aslist로했다

public class MapPost { 
    String title; 
    Map<String,Boolean> categories; 

    public MapPost(String title, Map<String,Boolean> categories) { 
     this.title = title; 
     this.categories = categories; 
    } 
} 

Map<String, Boolean> categories = new HashMap<>(); 
categories.put("technology", true); 
categories.put("opinion", true); 
categories.put("cats", true); 
MapPost myMapPost = new MapPost("My great post", categories); 
+0

감사합니다 알렉스, firestore에서 메모를 발견했다 : subcollections 전체 쿼리는 현재 Cloud Firestore에서 지원되지 않습니다. 콜렉션에서 데이터를 조회해야하는 경우, 루트 레벨 콜렉션을 사용하십시오 ... 루트 레벨 콜렉션에 대해서는 어떤 것도 찾지 못합니다. – alserdar

+0

어느 것입니까? 그리고 무엇에 관한 것입니까? –

+0

내 의견을 alex ~ – alserdar