MongoDB 3.4 shell CRUD操作


dbを入力して、あなたが操作しているデータベースを表示します.データベースを切り替え、use dbNameを入力します.データベースが存在しない場合は、自動的に作成してくれます.show dbsを使用して、使用可能なすべてのデータベースを表示します.
テストデータは文末に
  • ドキュメント挿入操作の動作表現_idフィールド:MongoDBでは、コレクションに格納されているドキュメントに一意の_が必要です.idフィールドはprimary keyとして使用されます.ドキュメントに_が指定されていない場合idフィールド、MongoDBはObjectIdを_として使用します.idフィールドのデフォルト値;つまり、ドキュメントに挿入時に最上位レベルの_が含まれていない場合idフィールド、MongoDBドライバはObjectIdを持つ_を追加しますidフィールド.db.users.insertOne()の挿入:単一のドキュメントをセットに挿入 3.2新機能
    db.users.insertOne({name:"webb",age:23})
    挿入に成功すると、対応するObjectId:
    {"acknowledged" : true,"insertedId" :ObjectId("590b237b0207f4bae6db100a")}
    db.users.insertMany():複数のドキュメントをセットに挿入 3.2新機能
    db.users.insertMany([{name:"lebo",age:22},{name:"jack",age:24}])
    挿入に成功すると、対応するObjectId:
    {"acknowledged" : true,"insertedIds" : [ObjectId("590b24f00207f4bae6db100b"),ObjectId("590b24f00207f4bae6db100c")]}
    db.collection.insert()が返されます.複数のドキュメントを挿入するには、ドキュメント配列をメソッドに渡します.
    db.users.insert({name:"webb",age:24})
    この操作は、操作状態を含むWriteResultオブジェクトを返します.挿入ドキュメントは、次のWriteResultオブジェクトを正常に返しました:
    WriteResult({ "nInserted" : 1 })
    nInsertedフィールドは、挿入ドキュメントの合計数を示します.この操作にエラーが発生した場合、WriteResultオブジェクトにはエラー情報が含まれます.操作状態を含むBulkWriteResultオブジェクトを返します.挿入に成功したドキュメントは、次のBulkWriteResultオブジェクトを返します:
    db.users.insert([{name:"lebo",age:25},{name:"jack",age:26}])
  • クエリードキュメント条件クエリー現在のセットのすべてのデータを検索:
    BulkWriteResult({    "writeErrors" : [ ],    "writeConcernErrors" : [ ],    "nInserted" : 2,    "nUpserted" : 0,    "nMatched" : 0,    "nModified" : 0,    "nRemoved" : 0,    "upserted" : [ ]
    })
    クエリー条件を指定:
    db.users.find({})db.users.find()
    使用$in後のクエリー条件(or代替も使用可能):
    db.users.find({age:22})
    複合クエリー:
    db.users.find({age:{$in:[22,23]}})
    使用or:
    db.users.find({name:"ahn", age:22})
    クエリーを返すマッピングフィールドマッピングドキュメント:マッピングドキュメントは、すべての一致ドキュメントを返すフィールドを制限します.マッピングドキュメントは、どのフィールドが含まれているか、または除外されているフィールド:
    db.users.find({$or:[{status:"A"},{age:{$lt:30}}]})
    value:
    { field1: , field2:  ... }
    指定されたフィールドのみを返す:
    db.users.find({status:"A"}, {name:1, status:1})
    除外されたフィールドを返す:
    db.users.find({status:"A"}, {name:1, status:1,_id:0})
    埋め込まれたドキュメント内の指定されたフィールドを返す:
    db.users.find({status:"A"}, {name:0, status:0})
    クエリ値がnullまたは存在しないフィールド挿入テストデータ:
    db.users.find({status:"A"}, {name:1,status:1,"favorites.food":1})
    等しいフィルタ:2つのドキュメントを返す.クエリがsparseインデックスを使用している場合、クエリはnull値のみに一致し、存在しないフィールドには一致しません.
    db.users.insert([{ "_id" : 900, "name" : null },{ "_id" : 901 }])
    タイプスクリーニング:クエリはnullの値を含むnameフィールドのドキュメントにのみ一致します.
    db.users.find({name:null})
    存在行スクリーニング:nameフィールドのないドキュメントのみが返されます.
    db.users.find({name:{$type:10}})
  • 1またはtrue戻るドキュメントにフィールド
  • を含める
  • 0またはfalseこのフィールドを除外
  • 指定されたフィールドおよび_のみが返されます.idフィールド:
  • ドキュメントの動作を更新_idフィールド:設定すると更新できません.idフィールドの値は、異なるものを使用することはできません.idフィールド値の置換ドキュメントは、既存のドキュメントを置換します.≪ドキュメント・サイズ|Document Size|emdw≫:更新操作を実行すると、ドキュメントのサイズが増加し、ドキュメントに割り当てられた領域を超えた場合、更新操作はディスク上でドキュメントを再配置します.ドキュメント内の指定フィールドdb.collection.updateOne()の更新:一致フィルタ条件の最初の一致するドキュメントの更新 3.2新機能$setオペレータを使用して指定したフィールドを更新$currentDateオペレータを使用してlastModifiedフィールドの値を現在時刻db.users.updateOne({"name":"sue"},{$set:{type:3},$currentDate:{lastModified:true})db.collection.updateMany():フィルタ条件に一致するすべてのドキュメントを更新 3.2新機能db.users.updateMany({"name":"lebo"},{$set:{age:22},$currentDate:{lastModified:true}})db.collection.update():フィルタ条件に一致する最初の一致するドキュメントdb.users.update({name:{webb"},{$set:{age:21},$currentDate:{lastModified:true})を更新db.collection.replaceOne():フィルタ条件に一致する最初のドキュメントを新しいドキュメントに置き換えます. 3.2新機能db.users.replaceOne({name:[webb]},{name:[webb],age:21})db.collection.update():フィルタ条件に一致する最初のドキュメントを新しいドキュメントに置き換えます.db.users.update({name:"xyz"},{name:"mee", age:25, type:1, status:"A"} )ドキュメントを削除すべてのドキュメントdb.collection.deleteMany()を削除 3.2新機能db.users.deleteMany({})db.collection.remove()db.users.remove({})条件を満たすすべてのドキュメントdb.collection.deleteMany()を削除 3.2新機能db.users.deleteMany({name:"lebo"})db.collection.remove():typeが1のすべてのドキュメントを削除します.db.users.remove({type:1})条件を満たすドキュメントdb.collection.deleteOne()のみを削除します. 3.2新機能db.users.deleteOne({name:“webb”})db.collection.remove():db.users.remove({name:“webb”},1)テストデータ:db.users.insertMany([ {   _id: 1,   name: "sue",   age: 19,   type: 1,   status: "P",   favorites: { artist: "Picasso", food: "pizza" },   finished: [ 17, 3 ],   badges: [ "blue", "black" ],   points: [       { points: 85, bonus: 20 },       { points: 85, bonus: 10 }    ]  },  {   _id: 2,   name: "bob",   age: 42,   type: 1,   status: "A",   favorites: { artist: "Miro", food: "meringue" },   finished: [ 11, 25 ],   badges: [ "green" ],   points: [       { points: 85, bonus: 20 },       { points: 64, bonus: 12 }    ]  },  {   _id: 3,   name: "ahn",   age: 22,   type: 2,   status: "A",   favorites: { artist: "Cassatt", food: "cake" },   finished: [ 6 ],   badges: [ "blue", "red" ],   points: [       { points: 81, bonus: 8 },       { points: 55, bonus: 20 }    ]  },  {   _id: 4,   name: "xi",   age: 34,   type: 2,   status: "D",   favorites: { artist: "Chagall", food: "chocolate" },   finished: [ 5, 11 ],   badges: [ "red", "black" ],   points: [       { points: 53, bonus: 15 },       { points: 51, bonus: 15 }    ]  },  {   _id: 5,   name: "xyz",   age: 23,   type: 2,   status: "D",   favorites: { artist: "Noguchi", food: "nougat" },   finished: [ 14, 6 ],   badges: [ "orange" ],   points: [       { points: 71, bonus: 20 }    ]  },  {   _id: 6,   name: "abc",   age: 43,   type: 1,   status: "A",   favorites: { food: "pizza", artist: "Picasso" },   finished: [ 18, 12 ],   badges: [ "black", "blue" ],   points: [       { points: 78, bonus: 8 },       { points: 57, bonus: 7 }    ]  } ] )