I am in the need of to know is it possible to read the file content and store the contents as documents inside a collection in mongodb. If it s possible how to do that? If its not possible do there exist alternative way of doing that?
Thanks in advance.
You can use MongoDB APIs for whatever programming language that you're familiar with.
Check out this: http://api.mongodb.com/
You can add files to GridFS within mongo by using mongofiles.exe from command line. (https://docs.mongodb.com/v3.0/reference/program/mongofiles/)
We can put a file within the GridFS store by running the following command and arguments
C:\mongodb\bin>mongofiles.exe put \temp\Music.mp3
2016-12-17T10:05:05.048+0000 connected to: localhost
added file: \temp\Music.mp3
We can then drop in to the mongo shell and see the file within the fs.files collection:
C:\mongodb\bin>mongo.exe
MongoDB shell version: 3.2.10
connecting to: test
> show collections
fs.chunks
fs.files
> db.fs.files.find().pretty()
{
"_id" : ObjectId("58550dd18f96a237504521b3"),
"chunkSize" : 261120,
"uploadDate" : ISODate("2016-12-17T10:05:05.143Z"),
"length" : 5386368,
"md5" : "0ed99265aaf3a64ddf5df794e98b1cb3",
"filename" : "\\temp\\Music.mp3"
}
>
MongoDB GridFS also allows you to add extra meta data on the file document and allows extra indexes too to allow you to easily search for documents.
Related
I have a mongodb database with version 3.6.3. I have another mongodb database (on another machine) using version 4.4.5 with no documents in it. I want to put the data from the v3.6.3 into the v4.4.5 database. Can I safetly do this using mongoexport and then mongoimport or do I need to perform more steps?
Yes, mongoexport writes the documents out to a JSON file, and mongoimport can read that file and insert the documents to the new database.
These will transfer only the documents, but not index information. You many want to consider mongodump/mongorestore if you also need to move indexes.
I am using mongosqld adapter for mongo BI connection. We can generate mongo .drdl files for a specific collection or ALL collections in a database. However, I need to generate .drdl files for ALL collections WITHOUT the word "history" in it. Any way we could do that without manually providing the collection names without word "history" in it.
mongodrdl command for a specific collection is:
mongodrdl -d test -c mycollection -o myCollSchema.drdl
I have a directory with loads of small images (say png icons) which I want to store in my MongoDB without using GridFS, ideally using a bash script and the mongo shell alone. From what I read, that seems technical feasible, but I did not yet manage to get it working. The official documentation but the recipe seems incomplete.
According to Christoph Menge, GridFS is indeed not absolutely necessary for e.g. icons only.
Assume, I have a image file called foo.png which I would like to persist into the database icons, the GridFS solution would be this:
mongofiles -u "uploader" -p "notsogreat" --authenticationDatabase "admin" -d icons put foo.png
But how do I do that without GridFS within bash?
base64 foo.png > mongo -u "uploader" -p "notsogreat" --authenticationDatabase "admin" [SOME_MAGIC]
StackOverflow pages I consulted (in no particular order):
Store images in a MongoDB database
Store images in MongoDB
How can I add images in mongoDB?
Read image file into a MongoDB document's binary field via mongo shell script
How do I insert a binary file into mongodb using javascript?
Redirecting the output to a text file in MongoDB
How to add binary data with objectId to mongoDB?
Read a file from a mongo shell
Further reference(s):
https://itknowledgeexchange.techtarget.com/itanswers/how-to-store-images-in-a-mongodb-database/
I'm moving a website and Mongo database to a new server.
Website can't see the data.
Mongo is installed and running on the same server.
Shell and server are both 3.6.3
Mongo shell sees the database.
From the shell,
dbs returns the database name.
use dbname appears to work.
db.getCollectionNames() returns a reasonable collection name. Say ["myCollection"]
.
db.getCollection("myColection").getIndexes() returns
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "id",
"ns" : "myDB.myCollection"
}
]
But after that, I can't get any further.
db.myCollection.find() and
db.getCollection("myCollection").find() both return nothing.
Everything in the log looks correct, no errors.
Does that tell us anything?
Use below to backup
Mongodump --out <directory>
and below to restore
mongorestore <collectionName><path to the backup>
One of the features of Mongo is that you can create a database or collection just by mentioning it.
Turns out that someone had tried to import the data and failed. So when I looked at 'show dbs' and 'getCollectionNames', it looked like it was working.
The files I saw in the database directory were export files that hadn't actually been imported.
I am just getting familiar with MongoDB and its my first time to use it. I am using Ubuntu enviornment for the development. I installed the MongoDB as per the instructions mentioned in the tutorial available on the MongoDb website. They said that the data will be stored in the /data/db, now i have two question about this
1) Where do i need to make this folder? means which directory?
2) I made one directory in the root directory / and then i ran the mongo server. I made one database with the name of movies with use movies command and then put some collection in it like action and comedy and then each collection i saved some documents with db.action.insert and db.comedy.insert command and when i tried to find it in /data/db folder i found three files and one folder but there is no file with the name of movies so my question is with what name my database is saved.
Please guide me in this aspect.
Thanks
By default, Mongo stores all of its DB files in the root of the configured dbdir directory. Look into the directoryperdb configuration option if you want Mongo to create a separate directory for each database and place the database files within it.
The default data directory /data/db needs to be created by you if it hasn't been already.
You can do this in Ubuntu via the shell:
CTL+ALT+T to bring up a Terminal window:
cd /
sudo mkdir /data
cd data
sudo mkdir db
Note: you may also need to change ownership of the new directory to be owned by you:
sudo chown your_user_name /data
Note: my Mongo installation defaulted to a dbdir of /var/lib/mongodb
You can also choose a different data directory when you start Mongo:
mongod --dbdir some/dir
If Mongo is installed as a service, you should be checking the dbdir configuration option in your mongodb.conf file (again, when I installed Mongo, which I believe I did via the Software Center, it placed mongodb.conf at /etc/mongodb.conf)
Hope this helps
What are the 3 files you see? test*?
Here are the 2 more likely cases
a) your DB files are into another location
b) your data is in the test database
Go back to your shell and run:
use local
db.startup_log.find({},{startTimeLocal:1,cmdLine:1}).sort({$natural:-1}).limit(1).pretty()
{
"_id" : "mycomputer",
"startTimeLocal" : "Mon Oct 7 09:44:25.353",
"cmdLine" : {
"config" : "mongodb.conf",
"dbpath" : "./db",
"fork" : "1",
"journal" : "1",
"logpath" : "./logs/mongo.log",
"port" : 27017,
"rest" : "1"
}
}
=> dbpath tells you the location that was provided on the command line or config file
Again in the shell, run the following:
use test
show collections
use movies
show collections
If you see your collection names under test, you may not have used 'use movies' before creating the data.