কম্পিউটার

একটি নির্দিষ্ট মান ক্ষেত্র সহ MongoDB যৌগিক সূচক সেট করুন


এর জন্য, আমরা createIndex() ধারণাটি ব্যবহার করব এবং সূচক −

তৈরি করব
> db.compoundIndexDemo.createIndex({"StudentName":1,"StudentAge":1},{unique:true});
{
   "createdCollectionAutomatically" : true,
   "numIndexesBefore" : 1,
   "numIndexesAfter" : 2,
   "ok" : 1
}

আসুন প্রথমে নথি-

সহ একটি সংগ্রহ তৈরি করি
> db.compoundIndexDemo.insertOne({"StudentName":"Chris"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e084c1d25ddae1f53b62207")
}
> db.compoundIndexDemo.insertOne({"StudentName":"Chris","StudentAge":23});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e084c5625ddae1f53b62209")
}
> db.compoundIndexDemo.insertOne({"StudentName":"Chris","StudentAge":22});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e084c5b25ddae1f53b6220a")
}
> db.compoundIndexDemo.insertOne({"StudentName":"Chris","StudentAge":23});
2019-12-29T12:19:02.225+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: web.compoundIndexDemo index: StudentName_1_StudentAge_1 dup key: { : "Chris", : 23.0 } :
WriteError({
   "index" : 0,
   "code" : 11000,
   "errmsg" : "E11000 duplicate key error collection: web.compoundIndexDemo index:                StudentName_1_StudentAge_1 dup key: { : \"Chris\", : 23.0 }",
   "op" : {
      "_id" : ObjectId("5e084c5e25ddae1f53b6220b"),
      "StudentName" : "Chris",
      "StudentAge" : 23
   }
})
WriteError@src/mongo/shell/bulk_api.js:461:48
Bulk/mergeBatchResults@src/mongo/shell/bulk_api.js:841:49
Bulk/executeBatch@src/mongo/shell/bulk_api.js:906:13
Bulk/this.execute@src/mongo/shell/bulk_api.js:1150:21
DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:252:9
@(shell):1:1

Find() পদ্ধতির সাহায্যে একটি সংগ্রহ থেকে সমস্ত নথি প্রদর্শন করার জন্য নিম্নলিখিত প্রশ্ন রয়েছে -

> db.compoundIndexDemo.find().pretty();

এটি নিম্নলিখিত আউটপুট −

তৈরি করবে
{ "_id" : ObjectId("5e084c1d25ddae1f53b62207"), "StudentName" : "Chris" }
{
      "_id" : ObjectId("5e084c5625ddae1f53b62209"),
      "StudentName" : "Chris",
      "StudentAge" : 23
}
{
   "_id" : ObjectId("5e084c5b25ddae1f53b6220a"),
   "StudentName" : "Chris",
   "StudentAge" : 22
}

  1. MongoDB $slic একটি পরিসীমা সহ সেট করবেন?

  2. MongoDB-তে প্রতিটি কর্মচারীর 10 শতাংশের সাথে বেতন ক্ষেত্রের মান আপডেট করুন

  3. MongoDB save() পদ্ধতিতে পরিবর্তনশীল মান সেট করুন

  4. নির্দিষ্ট ক্ষেত্রের মান সহ নথি আনতে MongoDB সমষ্টি?