Last active
December 19, 2015 11:59
-
-
Save joaodubas/5951465 to your computer and use it in GitHub Desktop.
Querying an array property in a collection with tingodb.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var path = require('path'); | |
var engine = require('tingodb')({}); | |
var dbPath = path.join(__dirname, 'db'); | |
if (!fs.existsSync(dbPath)) { | |
fs.mkdirSync(dbPath); | |
} | |
var db = new engine.Db(path.join(__dirname, 'db'), {}); | |
var coll = db.collection('post'); | |
function byPagination() { | |
coll | |
.find() | |
.sort({'created_at': -1}) | |
.limit(5) | |
.skip(0) | |
.each(function _found(err, item) { | |
console.log('sorted', item); | |
}); | |
} | |
function byTag() { | |
coll.find({tags: 'test'}).each(function _found(err, item) { | |
console.log('tagged', item); | |
}); | |
} | |
function _counted(err, count) { | |
if (count) { | |
// byPagination(); | |
byTag(); | |
} else { | |
var posts = []; | |
for (var i = 0, l = 10; i < l; i += 1) { | |
posts.push({ | |
title: 'Post num: ' + i, | |
body: 'Post about the number ' + i, | |
tags: ['test', 'post', 'post-' + i], | |
created_at: new Date(2013, i % 3, (i % 4) + 1) | |
}); | |
} | |
coll.insert(posts, function _inserted(err, items) { | |
console.log(items.length); | |
// byPagination(); | |
byTag(); | |
}); | |
} | |
} | |
function _indexTags(err, index) { | |
console.log('Created index: ', index); | |
coll.count(_counted); | |
} | |
function _indexCreatedAt(err, index) { | |
console.log('Created index: ', index); | |
coll.ensureIndex({'tags': 1}, {'_tiarr':true}, _indexTags); | |
} | |
coll.ensureIndex({'created_at': 1}, _indexCreatedAt); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment