venerdì, settembre 25, 2015

MongoDB session


enrico@enrico-XPS13:~/mongodb$ mongo
MongoDB shell version: 3.0.6
connecting to: test

> show dbs
enrico     0.078GB
local      0.078GB
nodetest1  0.078GB
test       0.078GB
> use enrico
switched to db enrico
> for (var i=0;i<10 i="" p="" print="">0
1
2
3
4
5
6
7
8
9
> use blog
switched to db blog
> db
blog

> db.posts.insert({
... title: "My first post",
... authorName: "Alvise",
... authorEmail: "enricogiurin@gmail.com",
... pubDate: new Date
...
... });
WriteResult({ "nInserted" : 1 })
> db.posts.count()
1
> db.posts.findOne()
{
"_id" : ObjectId("5604925564958e61b7a10dda"),
"title" : "My first post",
"authorName" : "Alvise",
"authorEmail" : "enricogiurin@gmail.com",
"pubDate" : ISODate("2015-09-25T00:16:21.649Z")
}
> db.posts.insert({title: "another post", "_id": 123});
WriteResult({ "nInserted" : 1 })
> db.posts.count()
2
> db.posts.find()
{ "_id" : ObjectId("5604925564958e61b7a10dda"), "title" : "My first post", "authorName" : "Alvise", "authorEmail" : "enricogiurin@gmail.com", "pubDate" : ISODate("2015-09-25T00:16:21.649Z") }
{ "_id" : 123, "title" : "another post" }
> db.posts.find().pretty()
{
"_id" : ObjectId("5604925564958e61b7a10dda"),
"title" : "My first post",
"authorName" : "Alvise",
"authorEmail" : "enricogiurin@gmail.com",
"pubDate" : ISODate("2015-09-25T00:16:21.649Z")
}
{ "_id" : 123, "title" : "another post" }
> db.posts.insert({
... title: "My first post",
... author: {
... firstName: "Enrico",
... lastName: "Giurin",
... email: "enricogiurin@gmail.com"
... },
... tags: ["coding", "mongodb", "db"],
... pubDate: new Date
...
... });
WriteResult({ "nInserted" : 1 })
> db.posts.find().pretty()
{
"_id" : ObjectId("5604925564958e61b7a10dda"),
"title" : "My first post",
"authorName" : "Alvise",
"authorEmail" : "enricogiurin@gmail.com",
"pubDate" : ISODate("2015-09-25T00:16:21.649Z")
}
{ "_id" : 123, "title" : "another post" }
{
"_id" : ObjectId("5604946d64958e61b7a10ddb"),
"title" : "My first post",
"author" : {
"firstName" : "Enrico",
"lastName" : "Giurin",
"email" : "enricogiurin@gmail.com"
},
"tags" : [
"coding",
"mongodb",
"db"
],
"pubDate" : ISODate("2015-09-25T00:25:17.293Z")
}

> db.posts.find({title: "My fist post"});
> db.posts.find({title: "My first post"});
{ "_id" : ObjectId("5604925564958e61b7a10dda"), "title" : "My first post", "authorName" : "Alvise", "authorEmail" : "enricogiurin@gmail.com", "pubDate" : ISODate("2015-09-25T00:16:21.649Z") }
{ "_id" : ObjectId("5604946d64958e61b7a10ddb"), "title" : "My first post", "author" : { "firstName" : "Enrico", "lastName" : "Giurin", "email" : "enricogiurin@gmail.com" }, "tags" : [ "coding", "mongodb", "db" ], "pubDate" : ISODate("2015-09-25T00:25:17.293Z") }
> db.posts.find({title: "My first post"}).count()
2
> db.posts.find({title: /po/i}).count()
3
> db.posts.find({title: /pos/i}).count()
3
> db.posts.find({title: /first/i}).count()
2
> db.posts.find({title: /first/i},{title:1})
{ "_id" : ObjectId("5604925564958e61b7a10dda"), "title" : "My first post" }
{ "_id" : ObjectId("5604946d64958e61b7a10ddb"), "title" : "My first post" }
> db.posts.find({title: /first/i},{title:1, _id: 0})
{ "title" : "My first post" }
{ "title" : "My first post" }
> db.posts.find({title: /first/i},{title:1, _id: 0}).pretty()
{ "title" : "My first post" }
{ "title" : "My first post" }
> db.posts.find({"author.email": "enricogiurin@gmail.com"}).pretty()
{
"_id" : ObjectId("5604946d64958e61b7a10ddb"),
"title" : "My first post",
"author" : {
"firstName" : "Enrico",
"lastName" : "Giurin",
"email" : "enricogiurin@gmail.com"
},
"tags" : [
"coding",
"mongodb",
"db"
],
"pubDate" : ISODate("2015-09-25T00:25:17.293Z")
}
> db.posts.find({"author.email": "enricogiurin@gmail.co"}).pretty()
> db.posts.find({"author.email": /urin@gmail.co/}).pretty()
{
"_id" : ObjectId("5604946d64958e61b7a10ddb"),
"title" : "My first post",
"author" : {
"firstName" : "Enrico",
"lastName" : "Giurin",
"email" : "enricogiurin@gmail.com"
},
"tags" : [
"coding",
"mongodb",
"db"
],
"pubDate" : ISODate("2015-09-25T00:25:17.293Z")
}