EzDevInfo.com

tingodb

Embedded Node.js database upward compatible with MongoDB TingoDB | Embedded Node.js database with MongoDB API

Populate TingoDB with data for acceptance test

I have NodeJS app that uses MongoDB as database. I'm using native mongo driver (not mongoess).

The application allow users to work on projects and share them and the logic that decide which projects a user is allowed to see is built as mongo criteria selector.

In order to test that I've found TingoDB which looks like a great candidate for mocking the MongoDB to be able to run the real model code and check that it is working.

My question is what is the best way to load the initial data? keep it in separate file? Keep it as another model?

Thank you, Ido.


Source: (StackOverflow)

Mongoose findById returning null for first object when the object exists

Mongoose is returning null for the first object, _ID 0, in the DB even though it exists. I can retrieve the first post just fine using similar code, but I can not retrieve the first tag. I can retrieve all of my data with the exception of the first Tag, tag1 for the first post. If I add a second post with tags: tag4, tag5 and tag6 I can retrieve all of the data for the second post including tag4. If I point to tag1 in a later post it cannot be retrieved.

I have done some of searching and know that null being returned means that the record can't be found. I have also tried using find and findOne in addition to findById and get the same result. I can't figure out where I'm going wrong if the record exists. I'm sure there is probably a better way of doing it.

Thanks for the help.

I'm using mongoose, mongoose-simpledb, and tungus with node-webkit.

Save Tags:

$.each(tagArray, function(i, t) {
    var tags = db.Tags();
    tags.nextCount(function(err, count) {
        post.tags.push(count + i); //tag ID to post
    });
    tags.text= tagArray[i]; //tag text
    post.nextCount(function(err, count) {
        tags.posts.push(count); //post ID to tag
    });
    tags.save(function(err) {
        if (err) {throw err;}
        console.log('tag saved');
    });
});

Find Tags:

$.each(results[i].tags, function(j, t) {
    db.Tags.findById(results[i].tags[j], function(err, tag) {
        if (err) {return console.error(err);}
        if (!tag) {return console.log('Could not find tag...');}
        postContent += '<a rel='nofollow' href="">' + tag.text + '</a> ';
    });
});

Or if I use the following tag2 is returned instead of tag1 which is what should be returned.

db.Posts.
    find({}).
    populate('tags').
    exec(function(error, post) {
        console.log(post[0].tags[0].text);
    });

Tags Model

var ObjectId = require('mongoose-simpledb').Types.ObjectId;

exports.schema = {
    _id: Number,
    text: String,
    posts: [{type: ObjectId, ref: 'Posts', index: true}]
};

Tags DB

{"k":"0000000078","o":"0000000061","v":"001"}
{"_id":0,"_uid":1,"_dt":1409869458919,"_s":"c245621efdb176d3f4dd2db749590730"}
{"_id":0,"text":"Tag1","posts":[{"$wrap":"$oid","v":0}],"__v":0}
{"k":"0000000078","o":"0000000061","v":"001"}
{"_id":1,"_uid":1,"_dt":1409869458921,"_s":"80bc4453ee777de0177b27ba76ddc859"}
{"_id":1,"text":"Tag2","posts":[{"$wrap":"$oid","v":0}],"__v":0}
{"k":"0000000078","o":"0000000061","v":"001"}
{"_id":2,"_uid":1,"_dt":1409869458930,"_s":"12682b1c27ea57e8c09b87a2a6605510"}
{"_id":2,"text":"Tag3","posts":[{"$wrap":"$oid","v":0}],"__v":0}

Using find on the Tags DB:

db.Tags.findOne({
    _id: '0'
}, function(err, result) {
    if (err) return console.error(err);
    console.dir('findOne: ' +result.text);
    //throws error - Cannot read property 'text' of null
});

db.Tags.
findById(0, function(err, tag) {
    if (err) return console.error(err);
    console.log('tag by id: ' + tag);
    //returns - null
});

db.Tags.
find({}).
exec(function(error, tag) {
    console.log("find:" + tag[0]);
    //returns - ( _id: 0, text: 'Tag1', _v: 0, posts: [0] )
    //correctly finding the tag but not by id
});

If I change these find functions to find any tag other than the first tag, the correct information is returned.


Source: (StackOverflow)

Advertisements

Angular controller not display data from JSON

I am using Angular and TingoDB (Mongo) inside Node Webkit for a single page application. However I have a strange problem that I have been unable to resolve.

When I use an object literal (option 2) the data displays correctly in the html page. However changing the code to return data from the database (option 1) the results do not appear on the html page. I have converted both styles of data into the a JSON string to prove consistency and then using the angular.fromJSON to return an object. Both methods return the same JSON string in console.log and before anyone asks I have either Option 1 or Option 2 commented out so both are not running concurrently.

I have copied the JSON string based on the data passed from TingoDB into the console.log and re-entered it into the code below to ensure that no differences between the 2 versions of the data existed without changing any other code, but the problem still persists.

Can anyone shed light on why this occurs and how to fix it?

var app = angular.module('myApp', []);
var Engine = require('tingodb')(),
    assert = require('assert');

var db = new Engine.Db('./db', {});
var collection = db.collection("clean.db");

app.controller('tingoDataCtrl', ['$scope', function($scope) {



     function getData(callback) {
        //Option 1
          collection.find().toArray(function(err, docs){
                callback (JSON.stringify(docs));
           });

          //Option 2
           var docs = [
               {name:"tingo1", description:"56",_id:2},
               {name:"tingo2", description:"33",_id:3},
               {name:"tingo3", description:"22",_id:4},
               {name:"tingo4", description:"76",_id:5},
               {name:"tingo5", description:"99",_id:6}
           ];   
           callback (JSON.stringify(docs));
  }

    function info(b) {
        // I'm the callback
        console.log(b);
        $scope.items = angular.fromJson(b)
    }

    getData(info);

}]); 

And the Html

<body ng-app="myApp" id="main">

<div class="page page-data ng-scope">


    <section class="panel panel-default" ng-controller="tingoDataCtrl">
    <div class="panel-heading"><span class="glyphicon glyphicon-th"></span> Tingo Data</div>

        <table class="table">
            <thead>
            <th class="col-md-4">
              Name
            </th>
            <th class="col-md-8">
              Description
            </th>
            <th class="col-md-8">
              ID
            </th>
            <th></th>
            <tr>
            </tr>

            </thead>
            <tbody>
            <!-- <tr class="reveal-animation" ng-repeat="item in items | filter:query"> -->
            <tr ng-repeat="item in items | filter:query">
              <td>{{item.name}}</td>
              <td>{{item.description}}</td>
              <td>{{item._id}}</td>

            </tr>
            </tbody>
        </table>
    </section>
</div>
<script src="js/tingo_problem.js"></script>
 </body>

Source: (StackOverflow)

Querying for references in Tungus/TingoDB does not work when nativeObjectID is turned on

I am using the example from https://github.com/sergeyksv/tungus to replicate an issue I've been facing.

The data model is set up so a game contains a collection of references to Consoles.

My goal is to find all games that are available for a specific console (in this case there is exactly one). This works as long as I have nativeObjectID set to false (tingodb then uses integers instead of bson objectids). For several reasons I need the object id to be bson so I have to turn this on.

Running with nativeObjectID: false prints the game just as expected, having it set to true then returns null.

I'd be happy for any hints.

global.TUNGUS_DB_OPTIONS =  { nativeObjectID: true, searchInArray: true };

[..]

function example () {
  Game
  .findOne({ name: /^Legend of Zelda/ })
  .populate('consoles')
  .exec(function (err, ocinara) {
   if (err) return done(err);
   // console.log(ocinara);

   console.log(
    '"%s" was released for the %s on %s'
  , ocinara.name
  , ocinara.consoles[0].name
  , ocinara.released.toLocaleDateString());

var consoleId = ocinara.consoles[0]._id;

Game.find({where: {consoles: {_id: consoleId}}})
.exec(function(err, game) {
  console.log(err);
  console.log(game); //only filed when nativeObjectID is false
});

    done();
  })
}

for some reason the following works with bson object id and without:

Game.find().where('consoles').in([consoleId])
.exec(function(err, game) {
  console.log(err);
  console.log(game);
});

Source: (StackOverflow)

how to use TingoDB in stead of MongoDB with Deployd?

We want to use deployd on our homemade device running OpenWRT. As MongoDB would required too much memory, we want to use TingoDB in stead of MongoDB. How to configure Deployd to do so? Thanks a lot!


Source: (StackOverflow)

$pull an array element with TingoDB

I have been unsuccessful in using the $pull function to remove an array element in my TingoDB, although I can get $pop to remove either the first or last element of the array.

TingoDB supports the $pull function according to the test scripts so I wonder if the issue is with nested arrays, but cannot find any documentation to explain how to implement this.

The TingoDB support documentation states:

To enable nested arrays in individual queries for fields that do not use indexes, use "_tiarr." to prefix field names.

I'm using the following code to add an array:

collection.update({
    ITEMID: 3170
}, { $addToSet:{
    GROUPS: { GROUP: 236 }
}}, {
    upsert: false
});

and then using this code to remove the element:

collection.update({
     ITEMID: 3170
}, { $pull: { GROUPS: { GROUP: 236 }
}},
    upsert: false
); 

and the structure of the record is:

{
"ITEMID": "3170",
"KEY": false,
"GROUPS": [
    {
        "GROUP": 235
    },
    {
        "GROUP": 236
    },
    {
        "GROUP": 237
    }
],
"_id": {
    "$wrap": "$oid",
    "v": 2181
}

}


Source: (StackOverflow)

Using TingoDB within a Meteor application in development and production mode

I am trying to use TingoDB instead of MongoDB in a Meteor application I wrote for easier deployment.

I have been looking at the answer given here Meteor without mongo

However, this isn't proving to be so easy. For one thing, I would very much like to try and use (and debug) TingoDB in my development environment, not a bundled "production" environment. Hopefully when I bundle it for production the new "mongo-livedata.js" would be included too.

My biggest issue so far is that I have NO idea (and I've tried tracing and poking around Meteor) where the devil Meteor is getting the "correct" mongo-livedata.js package. I am hoping to be able to somehow "override" said package so I can "hack" away on it in safety and only for the single application I need it for. So far where Meteor is loading this for development has eluded me greatly. Let alone how I can, for this application, safely override it.

This is Meteor 1.1.02 -- the platform I am developing on is Mac OS X but I don't think that makes too much of a difference.

TIA!

Update: Using demeteorizer found the file in question now (mongo.js) and the similar code. However, changing over to TingoDB has now made the application non-reactive to data changes, rather the reason I was using Meteor in the first place. No idea why it's no longer reactive. :(


Source: (StackOverflow)

Problems running KeystoneJS without MongoDB

I would like to try running KeystoneJS without MongoDB.

There's a short blog post explaining how to do it at http://ifrederik.com/blog/2014/11/cms-without-db-running-keystonejs-without-mongodb/

Basically, it explains how to replace MondgoDB with TingoDB and using a Tungus driver.

The advice is to put the following into the top of the keystone.js file

global.TUNGUS_DB_OPTIONS = { nativeObjectID: true, searchInArray: true };
var tungus = require('tungus');
var mongoose = require('mongoose');

And later to set mongo database url to TingoDB.

keystone.set('mongo', 'tingodb://'+__dirname+'/data');

By doing this I got KeystoneJS up and running. By inspecting the contect of data/users file in TingoDB I can even see that the default user gets created, but I was not able to log in. It always reports that username / password combination is not ok.

What am I missing? How do I debug the problem to find out what exactly is the problem here?


Source: (StackOverflow)