Displaying data from mongoskin through HTML - mongodb

I am using mongodb to store data and i wrote a simple js script using mongoskin to query and retrieve data from a collection and it works fine...
var db = require('mongoskin').db('winter.ceit.uq.edu.au/openbeacon');
var time = 0;
var tagid = 1101;
db.collection('set1').find({tag : {'$elemMatch': {id: tagid,name :"reader07"}}},function(err, result) {
if (err) throw err;
result.each(function(err, band) {
console.log(band.tag);
time += band.time;
});
});
However i need a way to integrate this functionality into a webpage...so say i press a button on the page, the js script runs and the queried data is displayed on the webpage. When i try using this javascript in a HTML file, it erros saying "module not found" since im referencing the index.js for mongoskin and mongodb as the source in my html file.....
Please lemme know what are the ways (preferably the simplest ways) to do this.
thank you.

Try looking for example applications
here is one
https://github.com/christkv/tic-tac-toe-steps

Related

I want to drop only one table when ever sailsjs lift

I want to migrate only one table to drop whenever sails lift I wanted to delete that table. How it is possible to do? Or is there any way to delete table after sails lift?
I already tried to add migrate : 'drop' in that particular model but it won't work.
I am currently using sails 1.0 version
I found a way.
Sails Bootstrap
This runs before sails lifted
I drop the table here.
config/bootstrap.js
module.exports.bootstrap = async function() {
await Users.destroy({});
};
Manually Delete
if you want to delete collection manually, you can use this command in shell.
db.collection.drop()
check this link for more detail click here
Delete via Sails.js
to use low-level native code for mongodb
// Get access to the native MongoDB client via the default Sails datastore.
var db = sails.getDatastore().manager;
db.collection('users').drop()
find link here link
In sails.js structure there is one file called bootstrap which located in config folder.
bootstrap file execute before sails server is lift.
so whatever you wan't to do before server lifting you can write code in this bootstrap file.
module.exports.bootstrap = function(callback) {
Users.destroy({},(error,response)=>
{
if(error) { console.log("error while deleting table"); }
else { callback(); }
});
};

How can I "cache" a mongoDB/Mongoose result to be used in my Express.js views and routes

What I'm trying to achieve is some sort of way to cache results of a mongoDB/Mongoose query that I can use in my views and routes. I'd need to be able to update this cache whenever a new document is added to the collection. I'm not sure if this is possible and if it is then how to do it, due to how the functions are asynchronous
This is currently what I have for storing the galleries, however this is executed with every request.
app.use(function(req, res, next) {
Gallery.find(function(err, galleries) {
if (err) throw err;
res.locals.navGalleries = galleries;
next();
});
});
This is used to get gallery names, which are then displayed in the navigation bar from a dynamically generated gallery. The gallery model is setup with just a name of the gallery and a slug
and this is part of my EJS view inside of my navigation which stores the values in a dropdown menu.
<% navGalleries.forEach(function(gallery) { %>
<li>
<a href='/media/<%= gallery.slug %>'><%= gallery.name %></a>
</li>
<% }) %>
The website I'm working on is expected to get hundreds of thousands of concurrent users, so I don't want to have to query the database for every single request if not needed, and just update it whenever a new gallery is created.
Take a look at cachegoose. It will allow you to cache any query you want and invalidate that cache entry each time a new gallery is created.
You will need something like this:
const mongoose = require('mongoose');
const cachegoose = require('cachegoose');
cachegoose(mongoose); // You can specify some options here to use Redis instead of in-memory cache
app.get(function(req, res, next) {
...
Gallery
.find()
.cache(0, 'GALLERY-CACHE-KEY')
.exec(function(err, galleries) {
if (err) throw err;
res.locals.navGalleries = galleries;
next();
});
...
});
app.post(function(req, res, next) {
...
new Gallery(req.body).save(function (err) {
if (err) throw err;
// Invalidate the cache as new data has been added:
cachegoose.clearCache('GALLERY-CACHE-KEY');
});
...
});
Although you could do something simpler caching the results manually in a variable and invalidating that cache when new galleries are added, I would advise you to take a look at that package instead.
I've created modern library for handling cache clearing automatically. In short it's more advanced and faster than cachegoose, because it's also caching Mongoose Documents instances in memory.
Speedgoose has autoclearing plugin, which you can set on a given schema, So it's not only ttl-based. It works on mongoose document events to clear results related to the document. So if the result of query XYZ is cached, and it was containing the record that was edited/removed, it will be removed from the cache. Same with adding and removing documents from schema. Those events might affect every cached result in the collection. So they are clearing the cache in the scope of a given model. I should be rather enough in 90% of cases.
In near future it will have many more features. And it's pretty easy to setup!
https://www.npmjs.com/package/speedgoose
Setup
import {applySpeedGooseCacheLayer} from "speedgoose";
import mongoose from "mongoose";
applySpeedGooseCacheLayer(mongoose, {
redisUri: process.env.REDIS_URI
})
Cache something!
//Caching both - query result, and instances of related Mongoose Documents in memory. It supports sort, select,project...etc.
model.find({}).sort({fieldA : 1}).cacheQuery()
//Caching only result
model.find({}).lean().cacheQuery()
//Works also with aggregation
model.aggregate([]).cachePipeline()

Querying Mongo returns empty array in Template.foo.onCreate in Meteor app

I get this code in my Meteor project, in a client/main.js file
Template.panel.onCreated(function loginOnCreated() {
var profile = Session.get('profile');
this.myvar = new ReactiveVar(User.find({}).fetch());
});
And the result of User.find({}) is empty. If I Query this anywhere else (including meteor mongo) I get an Array of users.
So I wonder if it is a problem with the fact that this code is running in client side. In this same file I get this query working in other places, but probably in the server context.
How can I populate this ReactiveVar with the Mongo result as soon as the Template/page is loaded?
If I do something like in Meteor.startup() at Server side:
console.log(User.find({}).count());
It gives me the correct number of Users. Immediately.
#edit
If I just add a setTimeout of a few seconds (it can't be jsut 1 second, it needs a longet time), it works in this very same place.
Template.panel.onCreated(function loginOnCreated() {
//...
setTimeout(function(){
template.timeline.set(User.find({}).fetch());
console.log(timeline)
},3000);
});
So, anyone knows why it takes so long to allow me to do this operation? Any workaround?
User.find({}).fetch() will give list of users on server side only.
You can probably write a meteor method for fetching the user list on server side and give it call using meteor.call.
In the callback function to this call you can assign the result to desired variable.

meteor: database is undefined

I'm having some troubles understanding, what i believe is trivial but i can't seem to get my head around it.
I have this publish function in server.js (server only)
Meteor.publish("tikiMainFind", function(){
return tikiDB.find()
})
In app.js (server + client) i'm declaring this mongo collection:
tikiDB = new Mongo.Collection("tiki")
Why is it that this doesn't work in client.js
console.log(tikiDB.find())
//ReferenceError: tikiDB is not defined
Without any idea how you have your app structured, I agree with David Weldon's answer. Check File Load Order to see what order your files are getting loaded.

What's the easiest way to store a large object in Meteor using GridFS?

I'm trying to backup a Lunr Index to Mongo (daily), and because it's running around 13MB, I'm triggering MongoError: document is larger than capped size errors. I'd like to use GridFS to get around the problem, but I'm having a heck of a time getting it to click.
In the simplest terms: Within Meteor, I'd like to save a 13MB JSON object to MongoDB using GridFS, and then be able to retrieve it when necessary -- all only on the server.
I've gone through the File-Collection and CollectionFS docs, and they seem far too complicated for what I'm trying to accomplish, and don't seem to address simply storing the contents of a variable. (Or, more likely, they do, and I'm just missing it.)
Here's what I'd like to do, in pseudo-code:
Backup = new GridFSCollection('backup');
var backupdata = (serialized search index data object);
Backup.insert({name:'searchindex', data:backupdata, date:new Date().getTime()});
var retrieved = Backup.findOne({name:'searchindex'});
Any suggestions?
var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
var GridStore = MongoInternals.NpmModule.GridStore;
var largedata = new GridStore(db,'name','w'); //to write
largedata.open(function(error,gs){
if (error) return;
gs.write('somebigdata',function(error,done){
if (error) return;
largedata.close();
})
});
var largedata = new GridStore(db,'name','r'); //to read data
largedata.open(function(error,gs){
if (error) return;
gs.read(function(error,result){
if (error) return;
//then do something with the result
largedata.close();
})
});
explanation
First you need the db object, which can be exposed via the MongoInternals https://github.com/meteor/meteor/tree/devel/packages/mongo
Second, you need the GridStore object, you get it from the MongoInternals as well
Then you can create a write or read object and follow the API http://mongodb.github.io/node-mongodb-native/1.4/markdown-docs/gridfs.html
The mongo used by Meteor is 1.3.x whereas the latest node mongodb native driver is 2.x.x http://mongodb.github.io/node-mongodb-native/2.0/api-docs/
So the API may change in the future. Currently, you need to do open and close and the callback are all async, you may want to wrap them with Meteor.wrapAsync (may not work on largedata.open), Future or Fiber