如何执行简单的 Couchbase Lite 查询

How to perform a simple Couchbase Lite query

我想知道如何进行一些简单的 CouchbaseLite 查询。

我有一个名为 "test" 的文档,其键值 "id": 5"name":"My first test"

我想搜索以下内容:

Database db = new Database("test_db");

using (var testDoc = new MutableDocument("test"))
{
      testDoc.SetInt("id", 5)
             .SetString("name", "My first test");

      db.Save(testDoc);
}

谢谢!

假设我正确理解你的问题,你想获取具有特定 属性 值的文档,只有当它存在时,你可以这样做

 var query = QueryBuilder.Select(SelectResult.expression(Meta.id),
                SelectResult.expression(Expression.property("id")))
    .From(DataSource.Database(db))
.Where(Expression.Property("name").NotNullOrMissing()
.And(Expression.Property("name").EqualTo(Expression.string("My first test"))))

或者,如果您知道文档的 ID,则对名称 属性 执行 GetDocument with the Id. Once you have the document, do a GetString 会更快。如果它 returns null,你可以认为它不存在。

请参阅有关 Couchbase Lite 查询基础知识的文档here.There are several examples. You may also want to check out API specs on NotNullOrMissing