Skip to content

Instantly share code, notes, and snippets.

View pablocattaneo's full-sized avatar

Pablo Cattaneo pablocattaneo

View GitHub Profile
npm install mongodb --save
@pablocattaneo
pablocattaneo / ordered_false.js
Created January 3, 2019 13:18
How to add next document although the previous fails in mongoDb? #mongoDb #mongoShell
db.collectionName.insertMany(
[ <document 1> , <document 2>, ... ],
{ ordered: false }
)
// Example
db.products.insertMany( [
{ _id: 10, item: "large box", qty: 20 },
{ _id: 11, item: "small box", qty: 55 },
{ _id: 11, item: "medium box", qty: 30 },
{ _id: 12, item: "envelope", qty: 100},
db.myCollection.drop()
use databaseName
db.dropDatabase()
@pablocattaneo
pablocattaneo / how_to_insert_more_than_one_document_in_mongodb.js
Created January 2, 2019 10:51
How to insert more than one document in mongodb? #mongoDb #mongoShell
db.collectionName.insertMany( [ <document 1> , <document 2>, ... ])
// example
db.patients.insertMany([{
"firstName": "Max",
"lastName": "Martinez",
"age": 29,
"history": [{
"disease": "cold",
@pablocattaneo
pablocattaneo / how_to_create_a_collection_in_mongodb.js
Last active January 3, 2019 10:38
How to create a collection in MongoDb? #mongoDb #mongoShell Source: createCollection method: https://www.udemy.com/mongodb-the-complete-developers-guide/learn/v4/t/lecture/11758316?start=0
// Creating a collection using lazy mode
db.collectionName
// Createing a collection using createCollection method
db.createCollection()
@pablocattaneo
pablocattaneo / how_to_list_all_databases_on_mongodb.js
Created January 2, 2019 10:44
How to list all databases on mongodb? #mongoDb #mongoShell
show dbs
@pablocattaneo
pablocattaneo / how_to_create_a_database_using_mongodb.js
Last active January 2, 2019 10:42
How to create a database using MongoDb? #mongoDb #mongoShell
use dabase_name
// Note: If you try to see all database using show dbs you will notice that the database create it won't be list. That is because databese only will show once it have a collection with at least one document.
@pablocattaneo
pablocattaneo / how_to_update_all_document_in_a_collection_in_mongodb.js
Created January 1, 2019 21:10
How to update all document in a collection in mongodb? #mongoDb #mongoShell
db.collectionName.updateMany({}, {$set: {"canEdit": true}})
db.collectionName.updateOne(
{ "name" : "Pizza Rat's Pizzaria" },
{ $set: {"_id" : 4, "violations" : 7, "borough" : "Manhattan" } },
);
#the first parameter is the filter, it mean that mongo is going to update the collection that match with this creteria