Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Mongo Db runs based on (Key, Values)
- For Mac
- Onde downloaded:
- in the terminal run these commands:
- from the root directory run
- `sudo mkdir data`
- `cd` in to the data directory
- `sudo mkdir db`
- Mongo requires these folder to run
- the next step is to find the mongo Db file:
- /Users/$USER/Desktop/mongodb-osx-x86_64-4.0.10/bin/mongod
- go to the `bin` folder.
- run the mongod file then the mongo file, these will launch a local instants of mongo db
- the other will open the mongo db CLI.
- *** If you encounter a 100 error, this means that the DB does not have write access to the data/db directory
- its a simple fix of giving it access
- `sudo chmod -R go+w data/db/`
- now rerun the commands
- Commands:
- once mongo is running in a terminal
- you can run `cls` which clears the terminal window.
- To build or access an existing database
- `use someDb`
- If the DB doesn't exist Mongo will create it and switch you to that db
- all mongo commands run inside of the DB will require
- `db` to be run then the database name `db.someDoc`
- `db.some.insertOne()` format the input like a JSON you can keep recreating the same thing.
- `db.some.insertOne({name:"Joe", age"23", userName:"JoeyJoe"})`
- *It is easier to do this in a IDE, but make sure whenb you copy in back over you have vertical quotation marks
- * other wise you will get a syntax error
- `db.some.find()` this will return all of the entries in the db
- `db.som.find({name: "Joe"})` if you pass in an arguement it will return only those Documents whoes name contains "Joe"
- The find is CASE SENSENTIVE if one entry is "Joe" and the other is "joe" only "Joe" will be returned
- if you want to update a document you can simply do it by
- db.some.update({_id: "1"},{$set:{email: "joe@email.com}})
- you can also update and $unset it
- db.people.update({_id: "1"},{$unset:{age: "does not matter"}})
- count `db.some.find().count`
- Kotlin and mongoDb:
- connecting to a mongoDb you will need to add the dependcies to Maven or Gradle
- rebuild the DM
- create a mongo driver file in your project
- `val mongoClient = MongoClient("127.0.0.0", 27017)`
- you will want to connect a new instance of the mongo client, pass in the arguements ("Host string", port Int)
- these can be found on the mongod in the terminal.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement