{"id":76690,"date":"2025-04-09T14:10:08","date_gmt":"2025-04-09T14:10:08","guid":{"rendered":"http:\/\/bangla.sitestree.com\/?p=76690"},"modified":"2025-04-10T02:16:15","modified_gmt":"2025-04-10T02:16:15","slug":"mongodb-array-operations","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=76690","title":{"rendered":"MongoDB: Array Operations"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>Try these Array Operations:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.posts.find({}, {_id:0, category:1, tags:1});\ndb.posts.updateMany({category:'Event'}, {$addToSet:{tags:\"Tech\"}});\ndb.posts.updateMany({category:'Event'}, {$push:{tags:\"Tech Event\"}});\ndb.posts.updateMany({category:'Event'}, {$pull:{tags:\"Tech\"}});\ndb.posts.updateOne( { likes: 4 }, { $pop: { tags: -1 } } )\ndb.posts.updateMany({}, {$pull:{tags:&#91;\"Tech\"]}});\n db.posts.updateMany({}, {$pull:{tags:\"Tech\"}});\ndb.posts.updateMany({}, {$pull:{tags:{$in:&#91;\"Tech\", \"Tech Event\"]}}});\nRun it several times: db.posts.updateMany({}, {$push:{tags:\"Tech Event\"}});\nCheck data: db.posts.find({}, {tags:1, _id:0});\nPull and then check data: \ndb.posts.updateMany({}, {$pull:{tags:{$in:&#91;\"Tech\", \"Tech Event\"]}}});\ndb.posts.find({}, {tags:1, _id:0});<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Use data from the URL below: <a href=\"https:\/\/www.w3schools.com\/mongodb\/mongodb_mongosh_insert.php\">https:\/\/www.w3schools.com\/mongodb\/mongodb_mongosh_insert.php<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.posts.insertMany(&#91;  \n  {\n    title: \"Post Title 2\",\n    body: \"Body of post.\",\n    category: \"Event\",\n    likes: 2,\n    tags: &#91;\"news\", \"events\"],\n    date: Date()\n  },\n  {\n    title: \"Post Title 3\",\n    body: \"Body of post.\",\n    category: \"Technology\",\n    likes: 3,\n    tags: &#91;\"news\", \"events\"],\n    date: Date()\n  },\n  {\n    title: \"Post Title 4\",\n    body: \"Body of post.\",\n    category: \"Event\",\n    likes: 4,\n    tags: &#91;\"news\", \"events\"],\n    date: Date()\n  }\n])<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Theory\/Concept:<\/strong><\/p>\n\n\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Try these Array Operations: Use data from the URL below: https:\/\/www.w3schools.com\/mongodb\/mongodb_mongosh_insert.php Theory\/Concept:<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-76690","post","type-post","status-publish","format-standard","hentry","category-root","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":76688,"url":"http:\/\/bangla.sitestree.com\/?p=76688","url_meta":{"origin":76690,"position":0},"title":"Try: MongoDB Regular Expresssions","author":"Sayed","date":"April 9, 2025","format":false,"excerpt":"^ : starts with $ : Ends with .* : any char, any number of times \/i : case insensitive Try the following Queries \u2022db.posts.find( { title: { $regex: \/4$\/ } } ); \u2022db.posts.find( { category: { $regex: \/^T.*\/ } } ); \u2022 db.posts.find( { category: { $not: \/^p.*\/ }\u2026","rel":"","context":"In &quot;Root&quot;","block_context":{"text":"Root","link":"http:\/\/bangla.sitestree.com\/?cat=1"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":6974,"url":"http:\/\/bangla.sitestree.com\/?p=6974","url_meta":{"origin":76690,"position":1},"title":"\u09ae\u0982\u0997\u09cb\u09a1\u09bf\u09ac\u09bf &#8211; \u0995\u09c1\u09df\u09c7\u09b0\u09bf \u09a1\u09cb\u0995\u09c1\u09ae\u09c7\u09a8\u09cd\u099f (MongoDB &#8211; Query Document)","author":"Author-Check- Article-or-Video","date":"March 25, 2015","format":false,"excerpt":"MongoDB \u0985\u09a8\u09c1\u09b8\u09a8\u09cd\u09a7\u09be\u09a8\u09ae\u09c2\u09b2\u0995 \u09ac\u09be Query \u09a1\u0995\u09c1\u09ae\u09c7\u09a8\u09cd\u099f \u09a8\u09df\u09a8 \u099a\u09a8\u09cd\u09a6\u09cd\u09b0 \u09a6\u09a4\u09cd\u09a4 \u00a0 \u00a0 find() \u09aa\u09a6\u09cd\u09a7\u09a4\u09bf MongoDB collection \u09a1\u09c7\u099f\u09be \u0985\u09a8\u09c1\u09b8\u09a8\u09cd\u09a7\u09be\u09a8 \u0995\u09b0\u09a4\u09c7 \u0986\u09aa\u09a8\u09be\u0995\u09c7 MongoDB \u098f\u09b0 find() \u09aa\u09a6\u09cd\u09a7\u09a4\u09bf \u09ac\u09cd\u09af\u09ac\u09b9\u09be\u09b0 \u0995\u09b0\u09a4\u09c7 \u09b9\u09ac\u09c7\u0964 \u00a0 \u09b8\u09bf\u09a8\u099f\u09cd\u09af\u09be\u0995\u09cd\u09b8\u0983 find()\u00a0 \u098f\u09b0 \u09ac\u09c7\u09b8\u09bf\u0995 \u09b8\u09bf\u09a8\u099f\u09cd\u09af\u09be\u0995\u09cd\u09b8 \u09ac\u09be \u09ac\u09be\u0995\u09cd\u09af-\u0997\u09a0\u09a8 \u09a8\u09bf\u09ae\u09cd\u09a8\u09b0\u09c2\u09aa\u0983 \u00a0 >db.COLLECTION_NAME.find() \u00a0 find() \u09aa\u09a6\u09cd\u09a7\u09a4\u09bf \u09b8\u09ac\u0997\u09c1\u09b2\u09cb \u09a1\u0995\u09c1\u09ae\u09c7\u09a8\u09cd\u099f\u0995\u09c7 \u098f\u0995\u099f\u09bf \u0985-\u0995\u09be\u09a0\u09be\u09ae\u09cb\u0997\u09a4 \u09ad\u09be\u09ac\u09c7 \u09aa\u09cd\u09b0\u09a6\u09b0\u09cd\u09b6\u09a8 \u0995\u09b0\u09ac\u09c7\u0964 \u00a0 \u00a0 \u00a0 \u00a0\u2026","rel":"","context":"In &quot;Root&quot;","block_context":{"text":"Root","link":"http:\/\/bangla.sitestree.com\/?cat=1"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":24497,"url":"http:\/\/bangla.sitestree.com\/?p=24497","url_meta":{"origin":76690,"position":2},"title":"Tech stocks rise; broader gains capped by Russia probe #Root","author":"Author-Check- Article-or-Video","date":"April 11, 2021","format":false,"excerpt":"Tech stocks rise; broader gains capped by Russia probe \"Nasdaq to a record high on Monday, while the Dow and the S&P were weighed down by losses in healthcare stocks.\" \"Declining issues outnumbered advancers on the NYSE by 1,469 to 1,308. On the Nasdaq, 1,625 issues fell and 1,049 advanced.\"\u2026","rel":"","context":"In &quot;Finance&quot;","block_context":{"text":"Finance","link":"http:\/\/bangla.sitestree.com\/?cat=50"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":74659,"url":"http:\/\/bangla.sitestree.com\/?p=74659","url_meta":{"origin":76690,"position":3},"title":"Python and MongoDB Operations. Code Example","author":"Sayed","date":"May 17, 2022","format":false,"excerpt":"#!\/usr\/bin\/env python# coding: utf-8# In[63]: import pymongo; # In[64]: import pymongomyclient = pymongo.MongoClient(\"mongodb:\/\/localhost:27017\/\")mydb = myclient[\"mydatabase\"] # In[65]: # Check if database exist # In[66]: print(myclient.list_database_names()) # In[67]: dblist = myclient.list_database_names()if \"mydatabase\" in dblist:\u00a0 print(\"The database exists.\") # In[68]: # Create a collection # In[69]: import pymongomyclient = pymongo.MongoClient(\"mongodb:\/\/localhost:27017\/\")mydb = myclient[\"mydatabase\"]mycol\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"http:\/\/bangla.sitestree.com\/?cat=1428"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76232,"url":"http:\/\/bangla.sitestree.com\/?p=76232","url_meta":{"origin":76690,"position":4},"title":"MongoDB: Insert, Collection, Aggregate, Lookup","author":"Sayed","date":"August 7, 2024","format":false,"excerpt":"db.orders.insertMany( [\u00a0 \u00a0{ \"_id\" : 1, \"item\" : \"almonds\", \"price\" : 12, \"quantity\" : 2 },\u00a0 \u00a0{ \"_id\" : 2, \"item\" : \"pecans\", \"price\" : 20, \"quantity\" : 1 },\u00a0 \u00a0{ \"_id\" : 3 \u00a0}] ) db.inventory.insertMany( [\u00a0 \u00a0{ \"_id\" : 1, \"sku\" : \"almonds\", \"description\": \"product 1\", \"instock\" :\u2026","rel":"","context":"In &quot;\u09ac\u09cd\u09b2\u0997 \u0964 Blog&quot;","block_context":{"text":"\u09ac\u09cd\u09b2\u0997 \u0964 Blog","link":"http:\/\/bangla.sitestree.com\/?cat=182"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":6972,"url":"http:\/\/bangla.sitestree.com\/?p=6972","url_meta":{"origin":76690,"position":5},"title":"\u09ae\u0999\u09cd\u0997\u09cb\u09a1\u09bf\u09ac\u09bf &#8211; \u0987\u09a8\u09cd\u09b8\u09be\u09b0\u09cd\u099f \u09a1\u09cb\u0995\u09c1\u09ae\u09c7\u09a8\u09cd\u099f (MongoDB &#8211; Insert Document)","author":"Author-Check- Article-or-Video","date":"March 25, 2015","format":false,"excerpt":"MongoDB - \u09a1\u0995\u09c1\u09ae\u09c7\u09a8\u09cd\u099f \u09b8\u09a8\u09cd\u09a8\u09bf\u09ac\u09c7\u09b6\u09a8 \u09b8\u09a8\u09cd\u09a8\u09bf\u09ac\u09c7\u09b6 \u09aa\u09a6\u09cd\u09a7\u09a4\u09bf MongoDB \u09b8\u0982\u0997\u09cd\u09b0\u09b9\u09c7 \u09a1\u09be\u099f\u09be \u09b8\u09a8\u09cd\u09a8\u09bf\u09ac\u09c7\u09b6\u09a8 \u09ac\u09be \u09aa\u09cd\u09b0\u09ac\u09c7\u09b6 \u0995\u09b0\u09a4\u09c7 \u09b9\u09b2\u09c7 \u0986\u09aa\u09a8\u09be\u0995\u09c7 MongoDB \u098f\u09b0 \u09b8\u09a8\u09cd\u09a8\u09bf\u09ac\u09c7\u09b6 \u09aa\u09a6\u09cd\u09a7\u09a4\u09bf \u09ac\u09cd\u09af\u09ac\u09b9\u09be\u09b0 \u0995\u09b0\u09a4\u09c7 \u09b9\u09ac\u09c7 \u0964 \u09b6\u09ac\u09cd\u09a6\u09ac\u09bf\u09a8\u09cd\u09af\u09be\u09b8 \u09b6\u09ac\u09cd\u09a6\u09ac\u09bf\u09a8\u09cd\u09af\u09be\u09b8 \u09b8\u09a8\u09cd\u09a8\u09bf\u09ac\u09c7\u09b6\u09a8 \u09ac\u09be \u09aa\u09cd\u09b0\u09ac\u09c7\u09b6\u09c7\u09b0 \u09aa\u09cd\u09b0\u09be\u09a5\u09ae\u09bf\u0995 \u09a8\u09bf\u09b0\u09cd\u09a6\u09c7\u09b6\u09a8\u09be \u09a8\u09bf\u09ae\u09cd\u09a8\u09c7 \u09a6\u09c7\u09df\u09be \u09b9\u09b2 : >db.COLLECTION_NAME.insert(document) \u0989\u09a6\u09be\u09b9\u09b0\u09a3 >db.mycol.insert({ _id: ObjectId(7df78ad8902c), title: 'MongoDB Overview', description: 'MongoDB is no sql database', by: 'tutorials\u2026","rel":"","context":"In &quot;Root&quot;","block_context":{"text":"Root","link":"http:\/\/bangla.sitestree.com\/?cat=1"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=76690"}],"version-history":[{"count":3,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76690\/revisions"}],"predecessor-version":[{"id":77765,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76690\/revisions\/77765"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=76690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=76690"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=76690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}