Realm with React Native, how to query through list objects

試著忘記壹切 提交于 2019-12-24 08:36:55

问题


I noticed this question (Use React Native Realm to Query through multiple List Objects) but was unable to get my query to work with the provided answer from @blackpla9ue.

I have the following schemas/models, and I want to query where ProductReview->productId is equal to 123, and Review->reviewText contains "a simple string".

This query simply returns all reviews for productId 123.

let allReviews = realm.objects('ProductReview', 'productId = "123");
let reviews = allReviews.filtered('reviews.reviewText CONTAINS[c] "a simple string"');

I have also tried removing 'productId = "123"' which simply results in all product reviews being returned for all products. It seems to either be completely ignoring filtered or I am missing something.

class ProductReview extends Realm.Object{}
ProductReview.schema = {
  name: 'ProductReview',
  primaryKey: 'productId',
  properties: {
      productId: 'string',
      averageRating: 'float',
      totalReviewCount: 'int',
      reviews: {type: 'list', objectType: 'Review'},

  }
}

class Review extends Realm.Object{}
Review.schema = {
   name: 'Review',
   properties: {
     rating: 'float',
     title: {type: 'string', optional: true},
     reviewText: {type: 'string', optional: true},
     userLocation: {type: 'string', optional: true},
     userNickName: {type: 'string', optional: true},
     tags: {type: 'list', objectType: 'StringList'}
   }
}

Looking to see what I might be doing wrong here.

Thanks!


回答1:


According to the docs, it seems to me that the query you're possibly looking for is

let allReviews = realm.objects('ProductReview');
let reviews = allReviews.filtered(
                 'productId = "123" AND reviews.reviewText CONTAINS[c] "a simple string"');



回答2:


In Realm

Full Text Search in React-native

Working

    realm = new Realm({
        schema: [StudentScheme]
    })
    const object1 = realm.objects('Student_Info');

    obj_filtered = object1.filtered("student_name CONTAINS $0" ,search );



    $0 second object $1 third object $2  .... etc 

like this

 let filteredData;
        if(search==undefined){
            filteredData = mydata.filtered('student_name CONTAINS ""');
        }else {
            //filteredData = mydata.filtered("student_name CONTAINS $0" ,search );
            filteredData = mydata.filtered("student_name CONTAINS $0 OR student_subject CONTAINS $1  OR student_class CONTAINS $2 " ,search ,search ,search);
        } 


来源:https://stackoverflow.com/questions/39944435/realm-with-react-native-how-to-query-through-list-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!