Last active
March 30, 2018 02:14
-
-
Save nishina555/084282749bce6ae1102c04c3a5e8c1b7 to your computer and use it in GitHub Desktop.
こんな時どうする? MongoDBクエリ逆引きリファレンス ref: https://qiita.com/nishina555/items/9e20211e8d6f12fdb7b7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"no": 1, | |
"firstName": "Taro", | |
"lastName": "Tanaka", | |
"age": 15, | |
"body": { "height": 165, "weight": 60 }, | |
"grades": [ | |
{ "semester": "first", "grade": 90 }, | |
{ "semester": "second", "grade": 80 } | |
], | |
"insertDate": ISODate("2016-10-20T15:00:00Z") | |
} | |
{ | |
"no": 2, | |
"firstName": "Satoshi", | |
"lastName": "Takeda", | |
"age": 16, | |
"body": { "height": 150, "weight": 55 }, | |
"grades": [ | |
{ "semester": "first", "grade": 90 }, | |
{ "semester": "second", "grade": 70 } | |
], | |
"insertDate": ISODate("2016-10-21T15:00:00Z") | |
} | |
{ | |
"no": 3, | |
"firstName": "Jiro", | |
"lastName": "Suzuki", | |
"age": 15, | |
"body": { "height": 170,"weight": 65 }, | |
"grades": [ | |
{ "semester": "first", "grade": 60 }, | |
{ "semester": "second", "grade": 80 } | |
], | |
"insertDate": ISODate("2016-10-22T15:00:00Z") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.コレクション名.find( { 条件 } ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.コレクション名.find( { 条件 } ).count() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.students.count() | |
3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.students.find( { "age":15 } ).count() | |
2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.コレクション名.find( { 条件 },{ 表示したいフィールド: 1, ...} ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.students.find( { }, { "_id": 0, "lastName": 1 } ) | |
{ "lastName" : "Tanaka" } | |
{ "lastName" : "Takeda" } | |
{ "lastName" : "Suzuki" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.コレクション名.find(オブジェクト.ネストされたオブジェクト) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.students.find( { "body.height": 150 } ) | |
{ "_id" : ObjectId("580b29bc24c83b73539eab46"), "no" : 2, "firstName" : "Satoshi", "lastName" : "Takeda", "age" : 16, "body" : { "height" : 150, "weight" : 55 }, "grades" : [ { "semester" : "first", "grade" : 90 }, { "semester" : "second", "grade" : 70 } ], "insertDate" : ISODate("2016-10-21T15:00:00Z") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.コレクション名.update( { 条件 }, { 演算子: { 配列名.$.フィールド } }, { multi: true } ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.students.update( { "lastName": "Tanaka", "grades.semester": "first" }, { $set: { "grades.$.grade": 95 } }, { multi: true } ) | |
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.students.update( { "lastName": "Tanaka" }, { $set: { "grades.$.grade": 95 } }, { multi: true } ) | |
WriteResult({ | |
"nMatched" : 0, | |
"nUpserted" : 0, | |
"nModified" : 0, | |
"writeError" : { | |
"code" : 16837, | |
"errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: grades.$.grade" | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.students.find( { "lastName": "Suzuki" } ) | |
{ "_id" : ObjectId("580b29bc24c83b73539eab47"), "no" : 3, "firstName" : "Jiro", "lastName" : "Suzuki", "age" : 15, "body" : { "height" : 170, "weight" : 65 }, "grades" : [ { "semester" : "first", "grade" : 60 }, { "semester" : "second", "grade" : 80 } ], "insertDate" : ISODate("2016-10-22T15:00:00Z") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.students.find( { 条件1, 条件2, ... } ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.students.find( { "firstName": "Jiro", "lastName": "Suzuki" } ) | |
{ "_id" : ObjectId("580b29bc24c83b73539eab47"), "no" : 3, "firstName" : "Jiro", "lastName" : "Suzuki", "age" : 15, "body" : { "height" : 170, "weight" : 65 }, "grades" : [ { "semester" : "first", "grade" : 60 }, { "semester" : "second", "grade" : 80 } ], "insertDate" : ISODate("2016-10-22T15:00:00Z") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.コレクション名.find( { $or [ {条件1}, {条件2}, .... ] } ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.コレクション名.find( { フィールド名: { $in: [ 値1, 値2, ...] } } ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.students.find( { $or: [ {"lastName": "Tanaka"}, {"lastName": "Suzuki"} ] } ) | |
{ "_id" : ObjectId("580b29bc24c83b73539eab45"), "no" : 1, "firstName" : "Taro", "lastName" : "Tanaka", "age" : 15, "body" : { "height" : 165, "weight" : 60 }, "grades" : [ { "semester" : "first", "grade" : 90 }, { "semester" : "second", "grade" : 80 } ], "insertDate" : ISODate("2016-10-20T15:00:00Z") } | |
{ "_id" : ObjectId("580b29bc24c83b73539eab47"), "no" : 3, "firstName" : "Jiro", "lastName" : "Suzuki", "age" : 15, "body" : { "height" : 170, "weight" : 65 }, "grades" : [ { "semester" : "first", "grade" : 60 }, { "semester" : "second", "grade" : 80 } ], "insertDate" : ISODate("2016-10-22T15:00:00Z") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.students.find( { "lastName": { $in: [ "Tanaka", "Suzuki" ] } } ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.students.find( { フィールド: /文字列/ } ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.students.find( { "firstName": /ro/ } ) | |
{ "_id" : ObjectId("580b29bc24c83b73539eab45"), "no" : 1, "firstName" : "Taro", "lastName" : "Tanaka", "age" : 15, "body" : { "height" : 165, "weight" : 60 }, "grades" : [ { "semester" : "first", "grade" : 90 }, { "semester" : "second", "grade" : 80 } ], "insertDate" : ISODate("2016-10-20T15:00:00Z") } | |
{ "_id" : ObjectId("580b29bc24c83b73539eab47"), "no" : 3, "firstName" : "Jiro", "lastName" : "Suzuki", "age" : 15, "body" : { "height" : 170, "weight" : 65 }, "grades" : [ { "semester" : "first", "grade" : 60 }, { "semester" : "second", "grade" : 80 } ], "insertDate" : ISODate("2016-10-22T15:00:00Z") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.students.find( { "lastName": /^Ta/ } ) | |
{ "_id" : ObjectId("580b29bc24c83b73539eab45"), "no" : 1, "firstName" : "Taro", "lastName" : "Tanaka", "age" : 15, "body" : { "height" : 165, "weight" : 60 }, "grades" : [ { "semester" : "first", "grade" : 90 }, { "semester" : "second", "grade" : 80 } ], "insertDate" : ISODate("2016-10-20T15:00:00Z") } | |
{ "_id" : ObjectId("580b29bc24c83b73539eab46"), "no" : 2, "firstName" : "Satoshi", "lastName" : "Takeda", "age" : 16, "body" : { "height" : 150, "weight" : 55 }, "grades" : [ { "semester" : "first", "grade" : 90 }, { "semester" : "second", "grade" : 70 } ], "insertDate" : ISODate("2016-10-21T15:00:00Z") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.コレクション名.find( { フィールド: { 比較演算子: 値 } } ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.コレクション名.update( { 条件 }, { 演算子: { 更新内容 } }, { multi: true } ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.students.find( { "body.height": { $gte: 160 } } ) | |
{ "_id" : ObjectId("580b29bc24c83b73539eab45"), "no" : 1, "firstName" : "Taro", "lastName" : "Tanaka", "age" : 15, "body" : { "height" : 165, "weight" : 60 }, "grades" : [ { "semester" : "first", "grade" : 90 }, { "semester" : "second", "grade" : 80 } ], "insertDate" : ISODate("2016-10-20T15:00:00Z") } | |
{ "_id" : ObjectId("580b29bc24c83b73539eab47"), "no" : 3, "firstName" : "Jiro", "lastName" : "Suzuki", "age" : 15, "body" : { "height" : 170, "weight" : 65 }, "grades" : [ { "semester" : "first", "grade" : 60 }, { "semester" : "second", "grade" : 80 } ], "insertDate" : ISODate("2016-10-22T15:00:00Z") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.コレクション名.find( { フィールド: ISODate("時間") } ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.students.find( { "insertDate": { $gte: ISODate("2016-10-22T00:00:00+09:00") , $lt: ISODate("2016-10-23T00:00:00+09:00") } } ) | |
{ "_id" : ObjectId("580b29bc24c83b73539eab46"), "no" : 2, "firstName" : "Satoshi", "lastName" : "Takeda", "age" : 16, "body" : { "height" : 150, "weight" : 55 }, "grades" : [ { "semester" : "first", "grade" : 90 }, { "semester" : "second", "grade" : 70 } ], "insertDate" : ISODate("2016-10-21T15:00:00Z") } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.コレクション名.update( { 条件 }, { 演算子 : { 更新内容 } }, false, true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.students.update( { "lastName": "Tanaka" }, { $set: { "age": 17 } } ) | |
WriteResult( { "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 } ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.コレクション名.remove( { 条件 } ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.students.remove( { "lastName": "Tanaka" } ) | |
WriteResult( { "nRemoved" : 1 } ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.students.remove( { } ) | |
WriteResult( { "nRemoved" : 3 } ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.コレクション名.count( { 条件 } ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment