【问题标题】:Realm with React Native, how to query through list objectsRealm with React Native,如何通过列表对象进行查询
【发布时间】:2016-10-09 14:01:15
【问题描述】:

我注意到了这个问题 (Use React Native Realm to Query through multiple List Objects),但无法让我的查询与 @blackpla9ue 提供的答案一起使用。

我有以下架构/模型,我想查询哪里 ProductReview->productId 等于 123,而 Review->reviewText 包含“一个简单的字符串”。

此查询仅返回 productId 123 的所有评论。

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

我还尝试删除 'productId = "123"',这只会导致所有产品的所有产品评论都被退回。它似乎要么完全忽略了filtered,要么我错过了一些东西。

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'}
   }
}

想看看我在这里可能做错了什么。

谢谢!

【问题讨论】:

    标签: database mobile react-native realm


    【解决方案1】:

    根据the docs,在我看来,您可能正在寻找的查询是

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

    【讨论】:

    • 嗨@EpicPandaForce,感谢您的回复。所以,基本上这让我走到了一半,只给我评论有“简单字符串”的产品。但我也得到了该 productId 的所有评论。我对 Realm 的了解是它确实只返回嵌套列表对象的过滤项。因为没有(当前)简单的方法可以说给我所有评论,其中包含父“productId”中的“简单字符串”,除非您实际上在评论列表对象中将其作为参考。我这样说对吗?
    • 该查询应该适用于您正在尝试做的事情。 @forstertime 你有一些可以用来重现问题的示例数据吗?如果有,请随时将其发送至 help@realm.io,我们可以查看。
    【解决方案2】:

    在领域

    Full Text Search in React-native

    工作

        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 
    

    像这样

     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);
            } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多