i'm trying to create collection from a script in mongodb, i found on this link a useful script to do that Make a script to create MongoDB collections from Mongo shell? (thanks for that script).
I'm using now this kind of script and it work fine:
db = db.getSiblingDB('emanueledb');
var collectionList = ["collection1", "collection2", "collection3"]
collectionList.forEach(function(collName) {
db.createCollection(collName, {autoIndexId: true});
});
I'm trying to cycle also some db but with this kind of code:
var dblist = ["emanueledb", "db_ema_dv"];
var collectionList = ["ab111", "bc111"];
dblist.forEach(function(dbname) { db.getSiblingDB(dbname)
collectionList.forEach(function(collName) {
db.createCollection(collName, {autoIndexId: true});
})
});
it works but create only the two collection on db_ema_dv. What is wrong?
Is also possible to get collection and db from an external csv to automate everything??
CSV example:
dbname,collectionname
emanueledb,collection1
emanueledb,collection2
testdb,testcollection
Thanks
In the code you posted this expression does nothing (it selects the database but you don't do anything with the database):
db.getSiblingDB(dbname)
Try:
db.getSiblingDB(dbname).createCollection(collName, {autoIndexId: true});
Perfect, thank you! Now works!
var dblist = ["emanueledb", "db_ema_dv"];
var collectionList = ["ab111111", "bc111111"];
dblist.forEach(function(dbname) {
collectionList.forEach(function(collName) {
db.getSiblingDB(dbname).createCollection(collName, {autoIndexId: true});
})
});
as requested before, is possibile to get db and collection from a csv?
CSV Example:
dblist,collectionList
emanueledb,collection1
emanueledb,collection2
testdb,testcollection
To get a database from a .csv you can use the mongoimport command. This will read a CSV file into a collection.
Related
I am working on a webpage which takes in input and depending on the user's inputted area code, my program will return data corresponding to this. My database is stored currently using MongoDB and I have attempted to query data using the find function and by connecting to my cluster. Below is the example of a section I have tried (with username and password hideen): Please let me know if anyone can find a solution to this, or if i am connecting this wrong. I am just a student so would appreciate any input.
Thank you!!
This is what i have currently tried, I intially tried to code in Python but this also did not work.
const uri = "mongodb+srv://<username>:<password>#cluster0.mongodb.net/data-police?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("set-1");
// perform actions on the collection object
collection.find({}).toArray(function(err, result) {
console.log(result);
client.close();
});
});
I am new to programming, and I heard that some guys on this website are quite angry, but please don't be. I am creating one web app, that has a web page and also makes som ecalculations and works with database (NeDB). I have an index.js
const selects = document.getElementsByClassName("sel");
const arr = ["Yura", "Nairi", "Mher", "Hayko"];
for (let el in selects) {
for (let key in arr) {
selects[el].innerHTML += `<option>${arr[key]}</option>`;
}
}
I have a function which fills the select elements with data from an array.
In other file named: getData.js:
var Datastore = require("nedb");
var users = new Datastore({ filename: "players" });
users.loadDatabase();
const names = [];
users.find({}, function (err, doc) {
for (let key in doc) {
names.push(doc[key].name);
}
});
I have some code that gets data from db and puts it in array. And I need that data to use in the index.js mentioned above, but the problem is that I don't know how to tranfer the data from getData.js to index.js. I have tried module.exports but it is not working, the browser console says that it can't recognize require keyword, I also can't get data directly in index.js because the browse can't recognize the code related to database.
You need to provide a server, which is connected to the Database.
Browser -> Server -> DB
Browser -> Server: Server provides endpoints where the Browser(Client) can fetch data from. https://expressjs.com/en/starter/hello-world.html
Server -> DB: gets the Data out of the Database and can do whatever it want with it. In your case the Data should get provided to the Client.
TODOs
Step 1: set up a server. For example with express.js (google it)
Step 2: learn how to fetch Data from the Browser(Client) AJAX GET are the keywords to google.
Step 3: setup a Database connection from you Server and get your data
Step 4: Do whatever you want with your data.
At first I thought it is a simple method, but them I researched a little bit and realized that I didn't have enough information about how it really works. Now I solved the problem, using promises and templete engine ejs. Thank you all for your time. I appreciate your help)
I am trying to store some data from an HTML formulary. I send the data using the HTTP POST method and I received them using Express framework in Node.js. The data arrives and it seems to work, but when I try to store them into MongoDB using Mongoose, the database is created but no data is stored when I execute DB.sis_dictionary.find()
I've tried to build different types of schemas and models, but none seems to work. And I get no error from Node.js, it seems to be working, but the MongoDB database does not store anything.
const Mongoose = require('mongoose');
Mongoose.connect('mongodb://localhost:27017/sis_dictionary', {useNewUrlParser: true});
const Schema = Mongoose.Schema;
const wordSchema = new Schema({
word: String
})
const Word = Mongoose.model('Word', wordSchema);
app.post('/saveWord', (req, res) => {
var word = new Word({word: String(req.body)});
word.save(function(err){
if(err) {
return console.error(err);
} else {
console.log("STATUS: WORKING");
}
})
console.log(req.body);
})
server.listen(3000);
console.log("SERVER STARTUP SUCCESS");
In the console, I get the message: "STATUS: WORKING".
sis_ditionary is your DB name and Words should be your collection name. As mongoose automatically creates a plural name for collection from a model if model name not specified when creating from a schema
db.collection.find() is a command to find a collection data when using mongo-shell. Run below command to get data:
use sis_dictionary
db.Words.find()
To beautify result use pretty method
db.Words.find().pretty()
First command will select DB and second command list collection data.
So when you execute db.sis_dictionary.find() it won't work because sis_dictinary is your DB name.
Nodejs way with 'mongoose'
//Model.find({});
Word.find({});
Also, check this line var word = new Word({word: String(req.body)});
What does req.body have? If req.body is {word:"example word"} then you directly pass req.body to modal constructor ie new Word(req.body);
According to your database URL, mongodb://localhost:27017/sis_dictionary, sis_dictionary is the database name.
And according to your mongoose model, Word is your collection name.
When you save a document, it saves under a collection. So you have to make a query under the collections.
So when you try to get data using DB.sis_dictionary.find(), definitely it won't work.
Your query should be like db.collection.find()
Use the following query,
use sis_dictionary
db.words.find()
// for better view
db.words.find().pretty()
For more please check the documentation.
Thank you everybody. You were all right, it was a problem related to my collections names. db.words.find().pretty() worked perfectly!The problem is solved.
I am new on nedb using node.js just wondering how to display all records or SELECT * FROM tablename i know that nedb is completely different from mysql but i need to embed a database inside my electron application, i just need to know if nedb is capable of db queries like mysql can do.
The code below able me to find a single records i just want to display all records.
var Datastrore = require('nedb');
var db = new Datastrore({filename: 'guitars.db'});
db.loadDatabase(function(err){
db.find({year : 1990}, function (err,docs){ console.log(docs); });
});
Just use
db.loadDatabase(function(err){
db.find({}, function (err,docs){
console.log(docs);//all docs
});
});
Here is the code for initialization
mongoose.connect('mongodb://localhost/gpsdb');
var db = mongoose.connection;
db.on('open', function () {
// now we can start talking
});
After successful opening, I am saving data like this, it's giving me no errors.
function saveGPSData(data){
var newData = new GPSData(data);
newData.save(function(err){
if(err)
return console.error(err);
});
}
Now in mongo shell, I am trying to retrieve that data but it's giving me empty output.
> use gpsdb
> db.GPSData.find();
>
It's giving me no output. Also can I found what models are there in gpsdb?
Here is the full source code http://pastebin.com/K7QPYAx8
JUST FOUND THAT in db folder there these files for my db created by mongodb
/data/db/gpsdb.0
/data/db/gpsdb.1
/data/db/gpsdb.n
A good place to start to get a quick answer is
https://groups.google.com/forum/#!forum/mongoose-orm
the community is very responsive :)
In the shell I did the following
>use gpsdb
switched to gpsdb
>db show collections
gpsdatas
From here I found that collection name is gpsdatas...... Not sure why its adding extra (s) to my modal, although you can see from the code that I am setting Modal to
var GPSData = mongoose.model('GPSData', GPSDataSchema);
Now using the shell its working like this
>db.gpsdatas.find()