我正在 android 中开发支持 mongoDB 的应用程序。 我想从 Java 代码创建 MongoID 并将其发送到我的服务器。
这是我的代码
我想创建这个json
{
"_id" : ObjectId("59b7bcdf92e706382b00009f"),
"user_id" : "6bb82a99-bccd-4868-a799-55e7d28f969c",
"is_active" : false,
"_slugs" : [
"aaa"
],
"facility_name" : "aaa",
"industry_id" : 1,
"old_industry_id" : 1,
"established_date" : "1994-06-01",
"summary" : "this is test",
"facility_website" : "www.xxx.com",
"contact" : {
"_id" : ObjectId("4637gdff92jhsgd378364y"),
"info_type" : "PROFILE_INFO",
"name" : "xxxxxxxxxx",
"mobileNumber" : "xxxxxxxxx",
"email" : "xxxxxxx@gmail.com",
"shown_on_profile" : true
}}
但我是从我的 java 代码中获取这个 Json。
{
"_id" : ObjectId("59b7bcdf92e706382b00009f"),
"user_id" : "6bb82a99-bccd-4868-a799-55e7d28f969c",
"is_active" : false,
"_slugs" : [
"aaa"
],
"facility_name" : "aaa",
"industry_id" : 1,
"old_industry_id" : 1,
"established_date" : "1994-06-01",
"summary" : "this is test",
"facility_website" : "www.xxx.com",
"contact" : {
"_id" : "{}",
"info_type" : "PROFILE_INFO",
"name" : "xxxxxxxxxx",
"mobileNumber" : "xxxxxxxxx",
"email" : "xxxxxxx@gmail.com",
"shown_on_profile" : true
}}
问题出在“联系人”对象上
这是我的java代码
String _id = new JsonObject().toString(); // this is Gson.JsonObject() object
JSONObject contact = new JSONObject();
contact.put("_id", _id);
contact.put("info_type", "PROFILE_INFO");
contact.put("name", "User-1");
contact.put("mobileNumber", "xxxxxxxxxx");
contact.put("email", "xxxxx@gmail.com");
contact.put("shown_on_profile", true);
最佳答案
MongoID 由服务器自动生成。您需要做的是在没有 MongoID 的情况下发送您的联系人。服务器会将数据插入数据库并返回刚刚创建的这个元素的 MongoID,您可以将整个联系人或仅返回 MongoID 给您的 Android 应用程序以使用它。
关于java - 如何在android代码中创建MongoID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46538711/