'How to query a field inside array sub-document using ottomanjs using query builder
I could't able to query using arrays sub-document field using ottomanJS and I cant find any documentation for query building for array sub-document, any help would be appreciated.
import { Schema, Query, getDefaultInstance } from "ottoman";
const TestSchema = new Schema({
block1:{
field1:{type: String, required: true},
field2:{type: String, required: true}
},
block2:[{
field3:{type: String, required: true},
field4:{type: String, required: true}
}]
})
const ottoman = getDefaultInstance();
const query = new Query({}, 'bucketName.scopeName.collectionName');
const where_exp = {block2[{field3}]:{$eq: 'xyz'}};
const result = query
.select()
.where(where_exp)
.build();
console.log(result);
Solution 1:[1]
const where_exp = {
$any: {
$expr: [{ b: { $in: ‘block2’ } }],
$satisfies: { ‘b.field3’: {$eq: “xyz”} },
}
};
//this will produce this valid N1Ql query SELECT * FROM travel-sample.inventory.hotel WHERE ANY b IN block2 SATISFIES b.field3=“xyz”
Here is the answer I got from couchbase forum incase anyone still looking for
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | noiissyboy |
