Skip to content

Instantly share code, notes, and snippets.

@bells17
Last active March 16, 2016 16:55
Show Gist options
  • Save bells17/2d12796b91a7d0b9a29b to your computer and use it in GitHub Desktop.
Save bells17/2d12796b91a7d0b9a29b to your computer and use it in GitHub Desktop.
Mongo Shell 徹底入門 基礎編 ref: http://qiita.com/bells17/items/8c1930f3cd9589f976a9
> db.users.findOne({_id: ObjectId("56e97630e62ae590e0d545a3")})
{
"_id" : ObjectId("56e97630e62ae590e0d545a3"),
"name" : "sample-user",
"password" : "password",
"created_at" : ISODate("2016-03-16T15:05:10.925Z"),
"updated_at" : ISODate("2016-03-16T15:05:10.925Z")
}
> db.users.findOne(ObjectId("56e97630e62ae590e0d545a3"))
{
"_id" : ObjectId("56e97630e62ae590e0d545a3"),
"name" : "sample-user",
"password" : "password",
"created_at" : ISODate("2016-03-16T15:05:10.925Z"),
"updated_at" : ISODate("2016-03-16T15:05:10.925Z")
}
> db.users.distinct("name")
[ "sample-user", "sample-user2", "sample-user-deleted" ]
> db.users.insert({name: "insert-user", password: "password", created_at: ISODate(), updated_at: ISODate() })
WriteResult({ "nInserted" : 1 })
> db.users.findOne({ name: "insert-user" })
{
"_id" : ObjectId("56e9807ee62ae590e0d545a6"),
"name" : "insert-user",
"password" : "password",
"created_at" : ISODate("2016-03-16T15:49:18.887Z"),
"updated_at" : ISODate("2016-03-16T15:49:18.887Z")
}
> db.users.update({"_id" : ObjectId("56e9807ee62ae590e0d545a6")}, {name: "updated-user", password: "password", created_at: ISODate(), updated_at: ISODate() })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.findOne({ name: "updated-user" })
{
"_id" : ObjectId("56e9807ee62ae590e0d545a6"),
"name" : "updated-user",
"password" : "password",
"created_at" : ISODate("2016-03-16T15:52:13.623Z"),
"updated_at" : ISODate("2016-03-16T15:52:13.623Z")
}
> db.users.update({"_id" : ObjectId("56e9807ee62ae590e0d545a6")}, { $set: { flags: { deleted: true } } })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.findOne({ name: "updated-user" })
{
"_id" : ObjectId("56e9807ee62ae590e0d545a6"),
"name" : "updated-user",
"password" : "password",
"created_at" : ISODate("2016-03-16T15:52:13.623Z"),
"updated_at" : ISODate("2016-03-16T15:52:13.623Z"),
"flags" : {
"deleted" : true
}
}
> db.users.remove({ _id : ObjectId("56e9807ee62ae590e0d545a6") })
WriteResult({ "nRemoved" : 1 })
> db.users.findOne(ObjectId("56e9807ee62ae590e0d545a6"))
null
> var user = { name: "saved-user", password: "password", created_at: ISODate(), updated_at: ISODate() }
> db.users.save(user);
WriteResult({ "nInserted" : 1 })
> user
{
"name" : "saved-user",
"password" : "password",
"created_at" : ISODate("2016-03-16T16:08:25.338Z"),
"updated_at" : ISODate("2016-03-16T16:08:25.338Z"),
"_id" : ObjectId("56e984fee62ae590e0d545a7")
}
> db.users.findOne(ObjectId("56e984fee62ae590e0d545a7"))
{
"_id" : ObjectId("56e984fee62ae590e0d545a7"),
"name" : "saved-user",
"password" : "password",
"created_at" : ISODate("2016-03-16T16:08:25.338Z"),
"updated_at" : ISODate("2016-03-16T16:08:25.338Z")
}
> var savedUser = db.users.findOne(ObjectId("56e984fee62ae590e0d545a7"))
> savedUser.flags = { deleted: true }
{ "deleted" : true }
> db.users.save(savedUser);
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.findOne(ObjectId("56e984fee62ae590e0d545a7"))
{
"_id" : ObjectId("56e984fee62ae590e0d545a7"),
"name" : "saved-user",
"password" : "password",
"created_at" : ISODate("2016-03-16T16:08:25.338Z"),
"updated_at" : ISODate("2016-03-16T16:08:25.338Z"),
"flags" : {
"deleted" : true
}
}
> var flag = true
> db.users.find().forEach(function(v) { flag = false })
> flag
false
> print({})
[object Object]
> printjson({})
{ }
> print("print string")
print string
> printjson("printstring")
"printstring"
> print([1, 2])
1,2
> printjson([1, 2])
[ 1, 2 ]
> db.users.find().sort({created_at: 1}).map(function(v) { return v.created_at; })
[
ISODate("2016-03-16T15:05:10.925Z"),
ISODate("2016-03-16T15:13:54.230Z"),
ISODate("2016-03-16T15:40:43.309Z"),
ISODate("2016-03-16T16:08:25.338Z")
]
> db.users.find().sort({created_at: -1}).map(function(v) { return v.created_at; })
[
ISODate("2016-03-16T16:08:25.338Z"),
ISODate("2016-03-16T15:40:43.309Z"),
ISODate("2016-03-16T15:13:54.230Z"),
ISODate("2016-03-16T15:05:10.925Z")
]
> db.users.find().limit(2)
{ "_id" : ObjectId("56e97630e62ae590e0d545a3"), "name" : "sample-user", "password" : "password", "created_at" : ISODate("2016-03-16T15:05:10.925Z"), "updated_at" : ISODate("2016-03-16T15:05:10.925Z") }
{ "_id" : ObjectId("56e97834e62ae590e0d545a4"), "name" : "sample-user2", "password" : "password", "created_at" : ISODate("2016-03-16T15:13:54.230Z"), "updated_at" : ISODate("2016-03-16T15:13:54.230Z") }
> db.users.find()
{ "_id" : ObjectId("56e97630e62ae590e0d545a3"), "name" : "sample-user", "password" : "password", "created_at" : ISODate("2016-03-16T15:05:10.925Z"), "updated_at" : ISODate("2016-03-16T15:05:10.925Z") }
{ "_id" : ObjectId("56e97834e62ae590e0d545a4"), "name" : "sample-user2", "password" : "password", "created_at" : ISODate("2016-03-16T15:13:54.230Z"), "updated_at" : ISODate("2016-03-16T15:13:54.230Z") }
> db.users.find().skip(2)
{ "_id" : ObjectId("56e97e7fe62ae590e0d545a5"), "name" : "sample-user-deleted", "password" : "password", "created_at" : ISODate("2016-03-16T15:40:43.309Z"), "updated_at" : ISODate("2016-03-16T15:40:43.309Z"), "flags" : { "deleted" : true } }
{ "_id" : ObjectId("56e984fee62ae590e0d545a7"), "name" : "saved-user", "password" : "password", "created_at" : ISODate("2016-03-16T16:08:25.338Z"), "updated_at" : ISODate("2016-03-16T16:08:25.338Z"), "flags" : { "deleted" : true } }
> db.users.find().skip(0).limit(2)
{ "_id" : ObjectId("56e97630e62ae590e0d545a3"), "name" : "sample-user", "password" : "password", "created_at" : ISODate("2016-03-16T15:05:10.925Z"), "updated_at" : ISODate("2016-03-16T15:05:10.925Z") }
{ "_id" : ObjectId("56e97834e62ae590e0d545a4"), "name" : "sample-user2", "password" : "password", "created_at" : ISODate("2016-03-16T15:13:54.230Z"), "updated_at" : ISODate("2016-03-16T15:13:54.230Z") }
>
> db.users.find().skip(2).limit(2)
{ "_id" : ObjectId("56e97e7fe62ae590e0d545a5"), "name" : "sample-user-deleted", "password" : "password", "created_at" : ISODate("2016-03-16T15:40:43.309Z"), "updated_at" : ISODate("2016-03-16T15:40:43.309Z"), "flags" : { "deleted" : true } }
{ "_id" : ObjectId("56e984fee62ae590e0d545a7"), "name" : "saved-user", "password" : "password", "created_at" : ISODate("2016-03-16T16:08:25.338Z"), "updated_at" : ISODate("2016-03-16T16:08:25.338Z"), "flags" : { "deleted" : true } }
> db.users.find().pretty()
{
"_id" : ObjectId("56e97630e62ae590e0d545a3"),
"name" : "sample-user",
"password" : "password",
"created_at" : ISODate("2016-03-16T15:05:10.925Z"),
"updated_at" : ISODate("2016-03-16T15:05:10.925Z")
}
{
"_id" : ObjectId("56e97834e62ae590e0d545a4"),
"name" : "sample-user2",
"password" : "password",
"created_at" : ISODate("2016-03-16T15:13:54.230Z"),
"updated_at" : ISODate("2016-03-16T15:13:54.230Z")
}
> db.users.find().toArray()
[
{
"_id" : ObjectId("56e97630e62ae590e0d545a3"),
"name" : "sample-user",
"password" : "password",
"created_at" : ISODate("2016-03-16T15:05:10.925Z"),
"updated_at" : ISODate("2016-03-16T15:05:10.925Z")
},
{
"_id" : ObjectId("56e97834e62ae590e0d545a4"),
"name" : "sample-user2",
"password" : "password",
"created_at" : ISODate("2016-03-16T15:13:54.230Z"),
"updated_at" : ISODate("2016-03-16T15:13:54.230Z")
}
]
> db.users.find({ name: { $in: [ "sample-user", "sample-user-deleted" ] } })
{ "_id" : ObjectId("56e97630e62ae590e0d545a3"), "name" : "sample-user", "password" : "password", "created_at" : ISODate("2016-03-16T15:05:10.925Z"), "updated_at" : ISODate("2016-03-16T15:05:10.925Z") }
{ "_id" : ObjectId("56e97e7fe62ae590e0d545a5"), "name" : "sample-user-deleted", "password" : "password", "created_at" : ISODate("2016-03-16T15:40:43.309Z"), "updated_at" : ISODate("2016-03-16T15:40:43.309Z"), "flags" : { "deleted" : true } }
> db.users.find({ $or: [ { "flags.deleted": true }, { name: "sample-user" } ] })
{ "_id" : ObjectId("56e97630e62ae590e0d545a3"), "name" : "sample-user", "password" : "password", "created_at" : ISODate("2016-03-16T15:05:10.925Z"), "updated_at" : ISODate("2016-03-16T15:05:10.925Z") }
{ "_id" : ObjectId("56e97e7fe62ae590e0d545a5"), "name" : "sample-user-deleted", "password" : "password", "created_at" : ISODate("2016-03-16T15:40:43.309Z"), "updated_at" : ISODate("2016-03-16T15:40:43.309Z"), "flags" : { "deleted" : true } }
{ "_id" : ObjectId("56e984fee62ae590e0d545a7"), "name" : "saved-user", "password" : "password", "created_at" : ISODate("2016-03-16T16:08:25.338Z"), "updated_at" : ISODate("2016-03-16T16:08:25.338Z"), "flags" : { "deleted" : true } }
> db.users.find().forEach(function(v) { print((Object.keys(v).map(function(k) { return v[k]; } )).join(",")); })
ObjectId("56e97630e62ae590e0d545a3"),sample-user,password,Thu Mar 17 2016 00:05:10 GMT+0900 (JST),Thu Mar 17 2016 00:05:10 GMT+0900 (JST)
ObjectId("56e97834e62ae590e0d545a4"),sample-user2,password,Thu Mar 17 2016 00:13:54 GMT+0900 (JST),Thu Mar 17 2016 00:13:54 GMT+0900 (JST)
> db.users.find().map(function(v) { return { id: v._id.valueOf(), name: v.name, password: v.password }; }).forEach(function(v) { print((Object.keys(v).map(function(k) { return v[k]; } )).join(",")); })
56e97630e62ae590e0d545a3,sample-user,password
56e97834e62ae590e0d545a4,sample-user2,password
> db.users.find().toArray().reduce(function(count, v) { return ++count; }, 0)
2
> db.users.count({ name: "sample-user" })
1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment