MongoDBはnull値にインデックスを付けることができますか?
はい、MongoDBはnull値に簡単にインデックスを付けることができます。まず、ドキュメントを使用してコレクションを作成しましょう-
> db.demo170.createIndex({"Value":1},{unique:true});
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.demo170.insert({"Value":100});
WriteResult({ "nInserted" : 1 })
> db.demo170.insert({"Value":null});
WriteResult({ "nInserted" : 1 })
> db.demo170.insert({"Value":90});
WriteResult({ "nInserted" : 1 })
> db.demo170.insert({"Value":78});
WriteResult({ "nInserted" : 1 }) find()メソッドを使用してコレクションからすべてのドキュメントを表示する-
> db.demo170.find();
これにより、次の出力が生成されます-
{ "_id" : ObjectId("5e369b789e4f06af551997da"), "Value" : 100 }
{ "_id" : ObjectId("5e369b7c9e4f06af551997db"), "Value" : null }
{ "_id" : ObjectId("5e369b809e4f06af551997dc"), "Value" : 90 }
{ "_id" : ObjectId("5e369b969e4f06af551997dd"), "Value" : 78 } 以下は、nullにインデックスを付けるためのクエリです-
> db.demo170.find().sort({Value:1}); これにより、次の出力が生成されます-
{ "_id" : ObjectId("5e369b7c9e4f06af551997db"), "Value" : null }
{ "_id" : ObjectId("5e369b969e4f06af551997dd"), "Value" : 78 }
{ "_id" : ObjectId("5e369b809e4f06af551997dc"), "Value" : 90 }
{ "_id" : ObjectId("5e369b789e4f06af551997da"), "Value" : 100 } -
MongoDBシェルで日付値をフォーマットしますか?
日付値をフォーマットするには、MongoDBで$dateToStringを使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo480.insertOne({id:1,"DueDate":new ISODate("2020-01-10")});{ "acknowledged" : true, "insertedId" : ObjectId("5e821056b0f3fa88e2279098") } >
-
MongoDBコレクションにインデックスを作成しますか?
インデックスを作成するには、MongoDBでcreateIndex()を使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo702.createIndex({"details.id":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, &q