MongoDB – Baby Steps

Antes de empezar

Por favor consultar: Data Modeling

Bases de datos

> show dbs

local 0.03125GB

Borrar una Base de Datos y todas sus Colecciones

var del = db.getSiblingDB("library");

del.dropDatabase();

{ "dropped" : "library", "ok" : 1 }
> use library

switched to db library
> db

library

Colecciones

> db.books.count();

0

Borrar

> db.books.drop();

> db.cheatsheets.drop();

> db.users.drop();

Guardar

> db.books.insert({ title: "Josoroma", url: "http://www.josoroma.com", comment: "Fine Poetry", tags: ["elegant", "smart"], created: new Date() });
> var book = {};
> book.title = "Sparkle";

Sparkle
> book.url = "http://facebook.com/josoroma.sparkle";

http://facebook.com/josoroma.sparkle
> book.comment = "Free Will";

Free Will
> book.tags = ["free", "open"];

[ "free", "open" ]
> book.created = new Date;

ISODate("2013-09-28T07:40:18.847Z")
> book.meta = {}

{ }
> book.meta.genre = "Fiction"

Fiction
> book.meta.year = "2013"

2013
> book

{
	"title" : "Sparkle",
	"url" : "http://facebook.com/josoroma.sparkle",
	"comment" : "Free Will",
	"tags" : [
		"free",
		"open"
	],
	"created" : ISODate("2013-09-28T07:40:18.847Z"),
	"meta" : {
		"genre" : "Fiction",
		"year" : "2013"
	}
}
> db.books.save(book);

Buscar

> db.books.find();

{ "_id" : ObjectId("524686be049dece7cd46c398"), "title" : "Josoroma", "url" : "http://www.josoroma.com", "comment" : "Fine Poetry", "tags" : [  "elegant",  "smart" ], "created" : ISODate("2013-09-28T07:35:26.120Z") }
{ "_id" : ObjectId("52468865049dece7cd46c399"), "title" : "Sparkle", "url" : "http://facebook.com/josoroma.sparkle", "comment" : "Free Will", "tags" : [  "free",  "open" ], "created" : ISODate("2013-09-28T07:40:18.847Z"), "meta" : { "genre" : "Fiction", "year" : "2013" } }
> db.books.find().forEach(printjson);

{
	"_id" : ObjectId("524686be049dece7cd46c398"),
	"title" : "Josoroma",
	"url" : "http://www.josoroma.com",
	"comment" : "Fine Poetry",
	"tags" : [
		"elegant",
		"smart"
	],
	"created" : ISODate("2013-09-28T07:35:26.120Z")
}
{
	"_id" : ObjectId("52468865049dece7cd46c399"),
	"title" : "Sparkle",
	"url" : "http://facebook.com/josoroma.sparkle",
	"comment" : "Free Will",
	"tags" : [
		"free",
		"open"
	],
	"created" : ISODate("2013-09-28T07:40:18.847Z"),
	"meta" : {
		"genre" : "Fiction",
		"year" : "2013"
	}
}
> db.books.find();

{ "_id" : ObjectId("524686be049dece7cd46c398"), "title" : "Josoroma", "url" : "http://www.josoroma.com", "comment" : "Fine Poetry", "tags" : [  "elegant",  "smart" ], "created" : ISODate("2013-09-28T07:35:26.120Z") }
{ "_id" : ObjectId("52468865049dece7cd46c399"), "title" : "Sparkle", "url" : "http://facebook.com/josoroma.sparkle", "comment" : "Free Will", "tags" : [  "free",  "open" ], "created" : ISODate("2013-09-28T07:40:18.847Z"), "meta" : { "genre" : "Fiction", "year" : "2013" } }
> db.books.find()[0]

{
	"_id" : ObjectId("524686be049dece7cd46c398"),
	"title" : "Josoroma",
	"url" : "http://www.josoroma.com",
	"comment" : "Fine Poetry",
	"tags" : [
		"elegant",
		"smart"
	],
	"created" : ISODate("2013-09-28T07:35:26.120Z")
}

Iterando sobre el cursor en un bucle

var cursor = db.books.find();
while ( cursor.hasNext() ) printjson( cursor.next() );

{
	"_id" : ObjectId("524686be049dece7cd46c398"),
	"title" : "Josoroma",
	"url" : "http://www.josoroma.com",
	"comment" : "Fine Poetry",
	"tags" : [
		"elegant",
		"smart"
	],
	"created" : ISODate("2013-09-28T07:35:26.120Z")
}
{
	"_id" : ObjectId("52468865049dece7cd46c399"),
	"title" : "Sparkle",
	"url" : "http://facebook.com/josoroma.sparkle",
	"comment" : "Free Will",
	"tags" : [
		"free",
		"open"
	],
	"created" : ISODate("2013-09-28T07:40:18.847Z"),
	"meta" : {
		"genre" : "Fiction",
		"year" : "2013"
	}
}

Array Index

> db.books.find()[0]._id;

ObjectId("524686be049dece7cd46c398")
> db.books.find()[0]._id.getTimestamp();

ISODate("2013-09-28T07:35:26Z")
> db.books.find().forEach(printjson);

{
	"_id" : ObjectId("524686be049dece7cd46c398"),
	"title" : "Josoroma",
	"url" : "http://www.josoroma.com",
	"comment" : "Fine Poetry",
	"tags" : [
		"elegant",
		"smart"
	],
	"created" : ISODate("2013-09-28T07:35:26.120Z")
}
{
	"_id" : ObjectId("52468865049dece7cd46c399"),
	"title" : "Sparkle",
	"url" : "http://facebook.com/josoroma.sparkle",
	"comment" : "Free Will",
	"tags" : [
		"free",
		"open"
	],
	"created" : ISODate("2013-09-28T07:40:18.847Z"),
	"meta" : {
		"genre" : "Fiction",
		"year" : "2013"
	}
}

ObjectId

> new ObjectId

ObjectId("524689f8049dece7cd46c39a")
> db.cheatsheets.insert({ name: "Cheat Sheet One" });

> db.cheatsheets.insert({ name: "Cheat Sheet Two" });

> db.cheatsheets.insert({ name: "Cheat Sheet Three" });
> db.cheatsheets.find();

{ "_id" : ObjectId("52468abc049dece7cd46c39b"), "name" : "Cheat Sheet One" }
{ "_id" : ObjectId("52468ac8049dece7cd46c39c"), "name" : "Cheat Sheet Two" }
{ "_id" : ObjectId("52468acd049dece7cd46c39d"), "name" : "Cheat Sheet Three" }
> db.books.find().forEach(printjson);

{
	"_id" : ObjectId("524686be049dece7cd46c398"),
	"title" : "Josoroma",
	"url" : "http://www.josoroma.com",
	"comment" : "Fine Poetry",
	"tags" : [
		"elegant",
		"smart"
	],
	"created" : ISODate("2013-09-28T07:35:26.120Z")
}
{
	"_id" : ObjectId("52468865049dece7cd46c399"),
	"title" : "Sparkle",
	"url" : "http://facebook.com/josoroma.sparkle",
	"comment" : "Free Will",
	"tags" : [
		"free",
		"open"
	],
	"created" : ISODate("2013-09-28T07:40:18.847Z"),
	"meta" : {
		"genre" : "Fiction",
		"year" : "2013"
	}
}

Relaciones

> db.users.insert({ name: "Josoroma" });
> var user = db.users.findOne({ name: "Josoroma" });
> user

{ "_id" : ObjectId("52468d67b652bc67ace97589"), "name" : "Josoroma" }
> db.books.insert({ title: "Flickr", url: "http://flickr.com/josoroma", userId: user._id });
> db.books.find()[2];

{
	"_id" : ObjectId("52468e31b652bc67ace9758a"),
	"title" : "Flickr",
	"url" : "http://flickr.com/josoroma",
	"userId" : ObjectId("52468d67b652bc67ace97589")
}
> book = db.books.find()[2];

{
	"_id" : ObjectId("52468e31b652bc67ace9758a"),
	"title" : "Flickr",
	"url" : "http://flickr.com/josoroma",
	"userId" : ObjectId("52468d67b652bc67ace97589")
}
> db.users.findOne({ _id: book.userId });

{ "_id" : ObjectId("52468d67b652bc67ace97589"), "name" : "Josoroma" }

Ver las colecciones de la Base de Datos library

show collections

books
cheatsheets
system.indexes
users
show dbs;

library	0.0625GB
local	0.03125GB