फ़ील्ड को जोड़ने के लिए, $concat ऑपरेटर का उपयोग करें। आइए पहले दस्तावेजों के साथ एक संग्रह बनाएं -
>db.concatenateFieldsDemo.insertOne({"StudentFirstName":"Adam","StudentLastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6ebf46d78f205348bc62e") } >db.concatenateFieldsDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6ebfc6d78f205348bc62f") } >db.concatenateFieldsDemo.insertOne({"StudentFirstName":"David","StudentLastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6ec376d78f205348bc630") } >db.concatenateFieldsDemo.insertOne({"StudentFirstName":"Sam","StudentLastName":"Williams"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6ec436d78f205348bc631") }
खोज () विधि की मदद से संग्रह से सभी दस्तावेजों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है -
> db.concatenateFieldsDemo.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
{ "_id" : ObjectId("5cd6ebf46d78f205348bc62e"), "StudentFirstName" : "Adam", "StudentLastName" : "Smith" } { "_id" : ObjectId("5cd6ebfc6d78f205348bc62f"), "StudentFirstName" : "John", "StudentLastName" : "Doe" } { "_id" : ObjectId("5cd6ec376d78f205348bc630"), "StudentFirstName" : "David", "StudentLastName" : "Miller" } { "_id" : ObjectId("5cd6ec436d78f205348bc631"), "StudentFirstName" : "Sam", "StudentLastName" : "Williams" }
फ़ील्ड चुनने और जोड़ने के लिए क्वेरी निम्नलिखित है -
> db.concatenateFieldsDemo.aggregate([ {$project:{"StudentFullName":{$concat: ["$StudentFirstName","/","$StudentLastName"]},"StudentFirstName":1,"StudentLastName":1}} ]);
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
{ "_id" : ObjectId("5cd6ebf46d78f205348bc62e"), "StudentFirstName" : "Adam", "StudentLastName" : "Smith", "StudentFullName" : "Adam/Smith" } { "_id" : ObjectId("5cd6ebfc6d78f205348bc62f"), "StudentFirstName" : "John", "StudentLastName" : "Doe", "StudentFullName" : "John/Doe" } { "_id" : ObjectId("5cd6ec376d78f205348bc630"), "StudentFirstName" : "David", "StudentLastName" : "Miller", "StudentFullName" : "David/Miller" } { "_id" : ObjectId("5cd6ec436d78f205348bc631"), "StudentFirstName" : "Sam", "StudentLastName" : "Williams", "StudentFullName" : "Sam/Williams" }