i have a problem with my app.
When written this db.request.count(); on mongo shell i have 4, but when i written Request.find().count() in my js code i have 0.
Someone can help me ?
Try using Request.count() instead of calling count() after find().
This is assuming the name of your model is Request.
The Mongoose documentation has examples here.
If you ran Request.find().count() on the client and got 0, it's most likely that your client-side code did not subscribe to the Request collection.
If you ran Request.find().count() on the server and got 0, make sure you connected to the correct database. I.e. Meteor creates its own Mongo instance on port 3001, whilst you may have been using a different database on 27017.
Related
I am trying to create a webpage that uses data from a MariaDB. My current idea (which has been giving me a lot of trouble) is to just connect to the database from the app.js file, which is the main script for my index.html.
const dotenv = require("dotenv");
dotenv.config();
const mariadb = require("mariadb");
const pool = mariadb.createPool({
database: process.env.DATABASE,
host: process.env.HOST,
user: process.env.USER_TOKEN,
password: process.env.PASSWORD,
});
// the rest of the code involves selecting from the db, and parsing the data
However, I have been running into many issues. I'm not too knowledgeable on all this, but I found that I need to webpack the file if I want to be able to use the "require" keyword. But I could not figure that out as I kept running into weird issues when using Browserify; I think there may be an incompatibility with MariaDB. I also looked into using JS modules, but I am not sure if that is possible with MariaDB.
I am trying to come up with another solution, potentially using some sort of API to a back end, which would make the GET request to the database, but I feel like it should not have to be that complicated for my sake (I also wouldn't really know where to start with this). All I basically want to do, is make a GET request to a MariaDB when the page loads on the client's browser and display that data on the webpage. Is there a simple way to do this?
I suggest you use nodejs to connect and query database as it will greatly resolve a lot of overhead for you..
The easiest way i can think of is using a prisma starter template here
https://github.com/prisma/prisma-examples/tree/latest/javascript/script
It also gives the added advantage of the ORM function...
Hope it helps.
I'm trying to write a mongo shell script that will delete some entries in an existing DB, and then reload them (depending on some conditions), and I'm having a lot of trouble.
One thing I can't understand/figure out is this:
To run the script file I'm making (let's call it script.js), I'm reading (from mongo docs and other places) that I need to use a command like this:
mongo mongodb://{{mycreds}}#{{address of existing mongo}}:27017/{{DB I want}}?authSource=admin script.js
However, since I'm connecting to that same DB in the script itself, I'm also seeing that I need to make that connection in the script.js:
db = connect(
"mongodb://{{mycreds}}#{{address where the existing mongo is}}:27017/{{DB I want}}?authSource=admin"
);
Why do I have to specify the DB connection when running the script? It's in the script?
Either way, I'm not able to make the connection in the script. I don't know if I'm running the script incorrectly, or making the connection to the DB in the script incorrectly. Or just something else.
In the docs, it says I can run the script in the mongo shell as well. But don't I have to have a mongo instance running at 27017 to start the script? And do I need a mongo running on my machine (since there's already one that exists that I need to connect to)?
I would really appreciate some clarity in this. All I'm trying to figure out is how to run a mongo script that connects to an existing DB, and I'm getting really tangled in all these docs.
Edit:
After receiving a comment (thanks Joe), I removed the connection in the script file and was able to make the connection. I guess having both was messing it up. I'd still like to be able to have the connection in the script, but not when I run the script.
I want to do this so that others can run the script without entering the long connection address.
If anyone knows a way to do this, I'd appreciate the help. Thanks.
you always need a credential for any DB you want to connect even if it's SQL. if you want to make it easier for you, you need to create a file.js in vs code and put a credential in a file when you need it you can just call it that's it, and use roboMongo to make your life easier with MongoDB.
I'm trying to replicate the latency compensation done by Meteor and minimongo. For instance to create an id on the client and then the same id on the server after calling the method, so the client can update the UI without waiting for the server response.
For this I need to generate the same Id on both the client and the server.
So, in meteor if I do: Random.createWithSeeds('abc').id()
I always get:
WKrBPwCSbzNHmhacn
But if I connect from and external app, outside of metor using a ddp client:
self.send({msg: 'method', id:id, randomSeed: 'abc', method: name, params: params});
I get a different Id. It's repeatable, but not the same as the one generated by Random. Why?
I cannot understand. Are they using a different generationId algorithm?
Packages I'm using:
On Meteor: https://atmospherejs.com/meteor/random
On external Client (outside Meteor): https://github.com/eddflrs/meteor-ddp + source code of random.js
This may not be a complete answer (I'm still looking too), but the way you're using Random.createWithSeeds should read:
> let generator = Random.createWithSeeds('abc')
> generator.id()
'WKrBPwCSbzNHmhacn'
> generator.id()
'h6iLWkdEfZ7wXWpPQ'
Perhaps an edit might clarify that createWithSeeds('abc') is supposed to return an object you call .id() on multiple times. I've never tried passing the seed from another ddp client though, and I'll let you know when I do
I am building a Node.js application, which reads from a MongoDB cluster. The application uses Mongoose to communicate with Mongo.
I would like to build a functionality, which is able to tell me, what does the mongoose know about the MongoDB replica set real-time (like calling rs.status()), but so far I was not able to find any kind of informations around the internet.
The purpose of this would be to be able to monitor, if something was changed in the replica set, and report it back if needed.
The problem is, I've found so far nothing on the internet in this subject. Does anyone have any idea on how to start it? It would be nice, if I could use the current mongoose connections for this purpose.
You can do this, but you need to be connected to the "admin" database and you will probably want a different connection for this other than what the rest of your application uses. Something like:
var mongoose = require("mongoose");
mongoose.connect(
"mongodb://localhost:27017,localhost:27018,localhost:27019/test");
var conn = mongoose.createConnection(
"mongodb://localhost:27017,localhost:27018,localhost:27019/admin");
conn.on("open",function() {
conn.db.command({"replSetGetStatus":1 },function(err,result) {
console.log( result );
});
});
It is important to wait for the connection to be established as well, hence the "event" callback. Mongoose will internally "queue" it's own methods operations until a connection is made, but this does not apply when grabbing a handle to the native db object and executing methods from that.
What is the order of operations for the mongodb-native driver?
Let's say you have a class that's purpose is to save a document of some sort and let's say it has a long life. How many times should open be called? Once per db write? When is close supposed to be called? Essentially I want a class method that looks like this:
var myMongoClass = new MongoDB(server,port)
myMongoClass.write_file(filename,callback)
myMongoClass.write_doc(doc,callback)
I posted this a while ago and got it working:
Problem with MongoDB GridFS Saving Files with Node.JS
It's now not working at all and failing with TypeError: Cannot read property 'md5' of null
Every time I work with this library I want to bang my head through a wall.
It seems like the correct answer is create a client and keep that client open for the duration of the application (never explicitly calling close). I have a wrapper that keeps a reference to the connected client and my app only boots up if the connection is received.