{"id":255739,"date":"2022-11-06T09:39:41","date_gmt":"2022-11-06T01:39:41","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=255739"},"modified":"2022-10-23T09:40:29","modified_gmt":"2022-10-23T01:40:29","slug":"mongodb-check","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mongodb-check.html","title":{"rendered":"\u5982\u4f55\u5728MongoDB\u4e2d\u68c0\u67e5\u5b57\u6bb5\u662f\u5426\u5b58\u5728"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5982\u4f55\u5728MongoDB\u4e2d\u68c0\u67e5\u5b57\u6bb5\u662f\u5426\u5b58\u5728\u3002\u6211\u4eec\u4f7f\u7528\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u57fa\u672c\u7684\u67e5\u8be2\u6765\u68c0\u67e5\u4e00\u4e2a\u5b57\u6bb5\u662f\u5426\u5b58\u5728\u3002\u4e5f\u91c7\u7528\u4e86com.mongodb.client.model.Filters\u548cDocument\u67e5\u8be2\u65b9\u6cd5\u6765\u68c0\u67e5\u5b57\u6bb5\u7684\u5b58\u5728\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5927\u5bb6\u597d\uff0c\u6211\u662f\u6307\u5317\u541b\u3002<\/p>\n

\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u6765\u804a\u804a\u5982\u4f55\u5728 MongoDB \u4e2d\u68c0\u67e5\u4e00\u4e2a\u5b57\u6bb5\u662f\u5426\u5b58\u5728\u3002<\/p>\n

\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Mongo\u6570\u636e\u5e93, \u7136\u540e\u653e\u5165\u4e00\u4e9b\u5047\u6570\u636e\uff0c\u4ee5\u4fbf\u5728\u4f8b\u5b50\u4e2d\u4f7f\u7528\u3002\u4e4b\u540e\uff0c\u6211\u4eec\u5c06\u5b9e\u6218\u4e00\u4e0b\u5982\u4f55\u5728 Mongo \u5ba2\u6237\u7aef\u4e2d\u8fdb\u884c\u67e5\u8be2\u4ee5\u53ca\u5728 Java \u4ee3\u7801\u4e2d\u68c0\u67e5\u5b57\u6bb5\u662f\u5426\u5b58\u5728\u3002<\/p>\n

\u793a\u4f8b\u914d\u7f6e<\/strong><\/div>\n

\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u628aMongo shell\u4e0a\u4e0b\u6587\u5207\u6362\u5230\u4e00\u4e2a\u5b58\u5728\u7684\u6570\u636e\u5e93\u3002<\/p>\n

use javanorth<\/pre>\n

\u6211\u4eec\u5c06\u5728users\u96c6\u5408\u4e2d\u63d2\u5165\u4e00\u4e2a\u7528\u6237\u3002<\/p>\n

db.users.insert({name: \"java\", surname: \"north\" })<\/pre>\n

\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u505a\u597d\u4e86\u51c6\u5907\u5de5\u4f5c\uff0c\u63a5\u4e0b\u53bb\u5c31\u8bb2\u8bb2\u5982\u4f55\u68c0\u67e5\u5b57\u6bb5\u662f\u5426\u5b58\u5728\u3002<\/p>\n

\u5728Mongo Shell\u4e2d\u68c0\u67e5\u5b57\u6bb5\u662f\u5426\u5b58\u5728<\/strong><\/div>\n

\u6211\u4eec\u80fd\u591f\u901a\u8fc7\u57fa\u672c\u7684\u67e5\u8be2\u6765\u68c0\u67e5\u7279\u5b9a\u5b57\u6bb5\u7684\u5b58\u5728\u4e0e\u5426\uff0c\u4f8b\u5982\u5728Mongo Shell\u6216\u5176\u4ed6\u6570\u636e\u5e93\u63a7\u5236\u53f0\u3002Mongo\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7279\u6b8a\u7684\u67e5\u8be2\u64cd\u4f5c\u7b26\uff0c$exists\u65b9\u4fbf\u6211\u4eec\u67e5\u8be2\u9a8c\u8bc1\u3002<\/p>\n

db.users.find({ 'name' : { '$exists' : true }})<\/pre>\n

\u6211\u4eec\u5148\u4f7f\u7528\u4e00\u4e2a\u6807\u51c6\u7684find\u200b \u65b9\u6cd5\uff0c\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u6307\u5b9a\u6211\u4eec\u8981\u67e5\u8be2\u7684\u5b57\u6bb5\uff0c\u5e76\u4f7f\u7528$exists\u200b\u67e5\u8be2\u64cd\u4f5c\u7b26\u3002\u5982\u679cname\u200b\u5b57\u6bb5\u5728users\u96c6\u5408\u4e2d\u5b58\u5728\uff0c\u6240\u6709\u5305\u542b\u8be5\u5b57\u6bb5\u7684\u8bb0\u5f55\u90fd\u5c06\u4f1a\u88ab\u8fd4\u56de\u3002<\/p>\n

>[\r\n  {\r\n    \"_id\": {\"$oid\": \"6115ad91c4999031f8e6f582\"},\r\n    \"name\": \"java\",\r\n    \"surname\": \"north\"\r\n  }\r\n]<\/pre>\n

\u5982\u679c\u8be5\u5b57\u6bb5\u4e0d\u5b58\u5728\uff0c\u6211\u4eec\u5c06\u5f97\u5230\u4e00\u4e2a\u7a7a\u7684\u7ed3\u679c\u3002<\/p>\n

\u5728Java\u4e2d\u68c0\u67e5\u5b57\u6bb5\u7684\u5b58\u5728\u6027<\/strong><\/div>\n

\u6211\u4eec\u5148\u6dfb\u52a0Mongo driver \u8fd9\u4e2aMaven\u7684\u4f9d\u8d56\u5305<\/p>\n

\r\n    org.mongodb<\/groupid>\r\n    mongo-java-driver<\/artifactid>\r\n    3.12.10<\/version>\r\n<\/dependency><\/pre>\n

\u7136\u540e\u901a\u8fc7\u4ee3\u7801\u6784\u5efa\u4e00\u4e2a\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/p>\n

MongoClient mongoClient = new MongoClient();\r\nMongoDatabase db = mongoClient.getDatabase(\"javanorth\");\r\nMongoCollection collection = db.getCollection(\"users\");<\/document><\/pre>\n
\u4f7f\u7528\u8fc7\u6ee4\u5668<\/strong><\/div>\n

com.mongodb.client.model.Filters\u200b\u662fMongo\u4f9d\u8d56\u7684\u4e00\u4e2a\u5de5\u5177\u7c7b\uff0c\u5305\u542b\u4e86\u5f88\u591a\u6709\u7528\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u5c31\u53ea\u9700\u8981\u4f7f\u7528exists()\u65b9\u6cd5\u3002<\/p>\n

Document nameDoc = collection.find(Filters.exists(\"name\")).first();\r\nassertNotNull(nameDoc);\r\nassertFalse(nameDoc.isEmpty());<\/pre>\n

\u9996\u5148\uff0c\u6211\u4eec\u5c1d\u8bd5\u4eceusers\u200b\u96c6\u5408\u4e2d\u5bfb\u627e\u5143\u7d20\uff0c\u5e76\u5f97\u5230\u7b2c\u4e00\u4e2a\u627e\u5230\u7684\u5143\u7d20\u3002\u5982\u679c\u6307\u5b9a\u7684\u5b57\u6bb5\u5b58\u5728\uff0c\u6211\u4eec\u5f97\u5230\u4e00\u4e2anameDoc\u6587\u6863\u4f5c\u4e3a\u54cd\u5e94\u3002<\/p>\n

\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u6765\u770b\u770b\u5f53\u6211\u4eec\u8bd5\u56fe\u627e\u5230\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u5b57\u6bb5\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002<\/p>\n

Document nameDoc = collection.find(Filters.exists(\"non_existing\")).first();\r\nassertNull(nameDoc);<\/pre>\n

\u5982\u679c\u6ca1\u6709\u627e\u5230\u5143\u7d20\uff0c\u6211\u4eec\u4f1a\u5f97\u5230\u4e00\u4e2a null \u7684Document\u4f5c\u4e3a\u54cd\u5e94\u3002<\/p>\n

\u4f7f\u7528 Document \u67e5\u8be2<\/strong><\/div>\n

com.mongodb.client.model.Filters\u200b\u7c7b\u5e76\u4e0d\u662f\u68c0\u67e5\u5b57\u6bb5\u5b58\u5728\u7684\u552f\u4e00\u65b9\u6cd5\u3002\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528com.mongodb.BasicDBObject:\u7684\u6765\u67e5\u8be2\u9a8c\u8bc1\u3002<\/p>\n

Document query = new Document(\"name\", new BasicDBObject(\"$exists\", true));\r\nDocument doc = collection.find(query).first();\r\nassertNotNull(doc);\r\nassertFalse(doc.isEmpty());<\/pre>\n

\u7ed3\u679c\u4e0e\u524d\u9762\u7684\u4f8b\u5b50\u76f8\u540c\u3002\u5982\u679c\u5143\u7d20\u88ab\u627e\u5230\u4e86\uff0c\u6211\u4eec\u4f1a\u6536\u5230\u4e00\u4e2a\u975enull\u7684Document\u3002<\/p>\n

\u5f53\u6211\u4eec\u8bd5\u56fe\u627e\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u5b57\u6bb5\u65f6\uff0c\u4ee3\u7801\u7684\u7ed3\u679c\u4e5f\u662f\u4e00\u6837\u7684\u3002<\/p>\n

Document query = new Document(\"non_existing\", new BasicDBObject(\"$exists\", true));\r\nDocument doc = collection.find(query).first();\r\nassertNull(doc);<\/pre>\n

\u5982\u679c\u6ca1\u6709\u627e\u5230\u4efb\u4f55\u5143\u7d20\uff0c\u6211\u4eec\u4f1a\u5f97\u5230\u4e00\u4e2anull \u7684 Document \u4f5c\u4e3a\u54cd\u5e94\u3002<\/p>\n

\u603b\u7ed3<\/strong><\/div>\n

\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5982\u4f55\u5728MongoDB\u4e2d\u68c0\u67e5\u5b57\u6bb5\u662f\u5426\u5b58\u5728\u3002\u6211\u4eec\u4f7f\u7528\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u57fa\u672c\u7684\u67e5\u8be2\u6765\u68c0\u67e5\u4e00\u4e2a\u5b57\u6bb5\u662f\u5426\u5b58\u5728\u3002\u4e5f\u91c7\u7528\u4e86com.mongodb.client.model.Filters\u548cDocument\u67e5\u8be2\u65b9\u6cd5\u6765\u68c0\u67e5\u5b57\u6bb5\u7684\u5b58\u5728\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5927\u5bb6\u597d\uff0c\u6211\u662f\u6307\u5317\u541b\u3002 \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u6765\u804a\u804a\u5982\u4f55\u5728 MongoDB \u4e2d\u68c0\u67e5\u4e00\u4e2a\u5b57\u6bb5\u662f\u5426\u5b58\u5728\u3002 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa […]<\/p>\n","protected":false},"author":1920,"featured_media":95922,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-255739","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/255739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=255739"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/255739\/revisions"}],"predecessor-version":[{"id":255741,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/255739\/revisions\/255741"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/95922"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=255739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=255739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=255739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}