使用 java 驱动程序为我的模式创建一个 mongodb 条目 [英] Creating a mongodb entry for my schema using the java driver

查看:36
本文介绍了使用 java 驱动程序为我的模式创建一个 mongodb 条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面显示了一个记录在mongodb中的"profile"对象.我知道这种类型的模式在 mongoDB 上是非常可能的,您可以在其中创建一个名为 "name""alias" 的内部对象.我自己已经完成并已经测试过了.

The below displays a "profile" object recorded in mongodb. I know this type of schema is very possible on mongoDB where you would create an inner object called "name" and "alias" inside your profile object. I have done it myself and tested this already.

{"name": {"name": "Peter", "show": false},
 "alias": {"alias":"GoofyDuck", "show": false}}

我觉得 "name""alias" 内部对象并不是真正必要的,因为我有一个名为 alias 的内部对象,带有字段名为 alias 并希望我的 "profile" 对象看起来像这样 - 这在 mongoDB 上甚至可能吗?如果是,请告诉我代码.

I feel that "name" and "alias" inner objects are not really necessary as I have an inner object called alias with a field called alias and would like my "profile" object to look like this instead -Is this even possible on mongoDB? Please show me with code if it is.

{{"name": "Peter", "show": false},
  {"alias":"GoofyDuck", "show": false}}

我也知道这是可能的,但并不真的想将它嵌入到数组中:

I also know that this is possible but don't really want to embed it into an array:

{[{"name": "Peter", "show": false},
  {"alias":"GoofyDuck", "show": false}]}

推荐答案

任何有效的 JSON 都是 Mongo 中的有效文档.由于中间代码块不是有效的 JSON(内部对象缺少键),因此它对 Mongo 无效.同样,第三个代码块也不是有效的 JSON:数组缺少一个键.

Anything that is valid JSON is a valid document in Mongo. Since the middle code block is not valid JSON (the inner objects are missing keys) it is not valid for Mongo. On the same note the 3rd code block is also not valid JSON: the array is missing a key.

我认为您正在寻找的架构设计可能是:

I think that the schema design you are looking for may be:

{
    name: "...",
    showName: true/false,
    alias: "...",
    showAlias: true/false
}

这篇关于使用 java 驱动程序为我的模式创建一个 mongodb 条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆