简体   繁体   中英

How to construct a PouchDB select call to return all items where a field exists and is not an empty string

Here is a PouchDB select call which correctly returns all documents from a PouchDB database which have a field called 'templateDetails' with a subfield called 'areaName' where the value of 'areaName' is an empty string

selector: { 'templateDetails.areaName': ''}

How can a change this selector so that it returns all documents where 'templateDetails.areaName' exists AND is NOT an empty string?

Simply use the $gt operator (see Query language ).

For example,

selector: { "templateDetails.areaName": { $gt: "" } }

This works because of the Collation Specification, best documented here .

Below is a snippet demonstrating such a selector in action. Document's with even numbered _id's have an empty string subfield value or a subfield that is a non-empty string, a non-string subfield, or no subfield at all.

 const gel = (id) => document.getElementById(id); const cel = (name) => document.createElement(name); // generate canned test documents function getDocsToInstall() { let docs = []; let i = 0; docs.push({ _id: i.toString(), field: null }); for (; i < 10; i++) { docs.push({ _id: i.toString(), field: { subfield: i % 2 === 0? "": i.toString() } }); } docs.push({ _id: (++i).toString(), field: { subfield: 9 } }); docs.push({ _id: (++i).toString(), field: { subfield: null } }); return docs; } let db; // init db instance async function initDb() { try { db = new PouchDB('test', { adapter: 'memory' }); await db.bulkDocs(getDocsToInstall()); } catch (err) { console.log(err.reason); } } function showDocs(tbodyId, docs) { const tbody = gel(tbodyId); docs.forEach(doc => { let el = cel("tr"); el.innerHTML = `<td>${doc._id}</td><td>${JSON.stringify(doc.field)}<td>`; tbody.appendChild(el); }); } initDb().then(async() => { // get all documents const alldocs = (await db.allDocs({ include_docs: true })).rows.map(row => row.doc); // display all document _id and field.subfield showDocs('alldocs', alldocs); // find documents where field.subfield have a non-empty text value. const result = await db.find({ selector: { "field.subfield": { $gt: "" } } }); // display all document _id and field.subfield showDocs('results', result.docs); });
 th, td { padding: .5em; text-align: left; min-width: 4em; } table { border-collapse: collapse; }
 <script src="//cdn.jsdelivr.net/npm/pouchdb@7.1.1/dist/pouchdb.min.js"></script> <script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb.memory.min.js"></script> <script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb.find.js"></script> <pre> <b>All Docs</b> <table> <tr><th>_id</th><th>field</th></tr> <tbody id="alldocs"></tbody> </table> </pre> <hr/> <pre> <b>Query: get all non-empty string subfields</b> { selector: { "field.subfield": { $gt: "" } } <table> <tr><th>_id</th><th>field</th></tr> <tbody id="results"></tbody> </table> </pre>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM