MongoDb toArray - mongodb

I am using mongodb and in the following code I am using mongodb find().toArray(), but it is giving me the error "Cannot read property 'toArray' of undefined"
req.activedb.collection('items').find().toArray(function (err, data) {
//...some code
})
whereas when I am using findOne(), then it is working properly.
req.activedb.collection('items').findOne(function (err, records) {
console.log(err, records); //Getting a single record here
})
req.activedb is my current db instance
Can you please tell me, what is missing here ?

I have resolved this issue.
Actually I was using mongoose to connect with my db, so it does not support find(). So now I am connecting to the db using new Db() method and it is working properly.

I have run belows command its work for me.
posts is nothing but collection.
db.posts.find().toArray();
You might be, you are making mistake in syntax .

Related

Meteor Angular2 and external changed of Mongodb

I play around with a simple Meteor Angular 2 app which only read from database. I have another app which change the database. But when the database is changed, I got error on Meteor app.
Exception in queued task: EXCEPTION: Error in client/match.html:0:19
ORIGINAL EXCEPTION: TypeError: Cannot read property 'league' of undefined
ORIGINAL STACKTRACE:
TypeError: Cannot read property 'league' of undefined
at DebugAppView._View_MatchComponent0.detectChangesInternal (MatchComponent.template.js:101:59)
at DebugAppView.AppView.detectChanges (http://localhost:3001/packages/modules.js?hash=5bea6cc36ccb7076b2a2834b250a3c141ff0cd78:57326:14)
at DebugAppView.detectChanges (http://localhost:3001/packages/modules.js?hash=5bea6cc36ccb7076b2a2834b250a3c141ff0cd78:57415:44)
at DebugAppView.AppView.detectViewChildrenChanges (http://localhost:3001/packages/modules.js?hash=5bea6cc36ccb7076b2a2834b250a3c141ff0cd78:57346:34)
at DebugAppView._View_MatchesComponent1.detectChangesInternal (MatchesComponent.template.js:106:8)
at DebugAppView.AppView.detectChanges (http://localhost:3001/packages/modules.js?hash=5bea6cc36ccb7076b2a2834b250a3c141ff0cd78:57326:14)
at DebugAppView.detectChanges (http://localhost:3001/packages/modules.js?hash=5bea6cc36ccb7076b2a2834b250a3c141ff0cd78:57415:44)
at DebugAppView.AppView.detectContentChildrenChanges (http://localhost:3001/packages/modules.js?hash=5bea6cc36ccb7076b2a2834b250a3c141ff0cd78:57341:37)
at DebugAppView._View_MatchesComponent0.detectChangesInternal (MatchesComponent.template.js:65:8)
at DebugAppView.AppView.detectChanges (http://localhost:3001/packages/modules.js?hash=5bea6cc36ccb7076b2a2834b250a3c141ff0cd78:57326:14)
ERROR CONTEXT:
[object Object]
I'm using autopublish which collection:
import {Mongo} from 'meteor/mongo';
export let MatchCollection = new Mongo.Collection('matches');
And angular component:
export class MatchesComponent
{
matches;
constructor() {
this.matches = MatchCollection.find();
}
}
Note: Meteor Blaze version works well. And I'm new to Meteor Angular2.
Thanks for all help.
I guess you are using *ngFor.
You can use this more stable way
this.matches = MatchCollection.find().fetch();
MatchCollection.find(); returns Mongo.Cursor<any>;
MatchCollection.find().fetch(); returns Array<any>
Weird, the bug should be already fixed...
It looks like the external change removed a property names league within the document, which your angular2 view uses somewhere.
Have a look at the document via meteor mongo to see how the document looks like in the storage before and after it has been modified by the external process.

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.

Displaying data from mongoskin through HTML

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

Meteor.js - Publish function not working: Coffeescript

I am having some trouble making the publish function work with Meteor. The code I am using is as follows:
Meteor.publish "adminArea", () ->
Meteor.users.find({
admin: true
}, {
fields: {
permissions: 1
}
})
and I am subscribing with:
Meteor.subscribe "adminArea"
This doesn't work though, when I run Meteor.user() in the console it just returns the default options.
If I run db.users.find({"admin": "true"}) in Mongo the correct information is returned.
The annoying thing is, this used to work perfectly until I reset my database with Meteor reset. Would this be messing it up or does anyone know what I am doing wrong now?
Thanks for any help.
I have now fixed this issue and it was complete error on my part. I had forgot to add the permissions field to the user in the database so when it ran the query, it would find admin: true but then be unable to return the permissions field because it didn't exist.
So note to self: Always add the necessary fields to the user.
Oops!
Thanks

how to implelement custom mongodb driver

I using mongodb in my poject and interesting how to implement simple mongodb driver which will allow to run mongodb command like in a shell form. Just writing command in javascript and pushing it to mongodb, then geting result and deserializing it.
Substitute your connection string in the code below.
While it would be extremely dangerous to provide this generally speaking (as it allows any command to execute), just call the eval function on the instance of the database that's returned.
This code is use the JavaScript/NodeJS driver:
var
mongodb = require('mongodb'),
MongoClient = mongodb.MongoClient
;
MongoClient.connect("mongodb://localhost:27017/default?w=1",
function (err, db) {
db.eval("db.version()", null, {noLock: true}, function(err, results) {
console.log(results);
});
});