MongoDB - Mongoose:文档结构/体系结构(MongoDB - Mongoose: document structure/architecture)

我有一个相对简单的问题,然而,我一直被卡住,一遍又一遍。 我认为这是我第四次尝试重新构建我的文档以尝试获得理想的结果。

我想用以下结构在猫鼬中创建一个数据库:

用户有许多帖子评论 帖子评论都属于用户 帖子可以属于很多 ,或者没有。 评论属于某个帖子

这是我到目前为止:

UserSchema

var userSchema = mongoose.Schema({

    local            : {
        email        : String,
        password     : String,
        username     : String,  
        created: {type:Date, default: Date.now} 

    }

});

PostSchema

var PostSchema   = new Schema({
    url: String, 
    highlighted: String, 
    comment: String, 
    image: String, 
    timeStamp: String, 
    description: String, 
    title: String,
    created: {type:Date, default: Date.now}, 
    private: Boolean, 
    favorite: Boolean, 
    readlater: Boolean, 
    postedBy: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    },
    comments: [{
        text: String,
        postedBy: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User'
        }
    }], 
    groups: [{
        name: String,
        postedBy: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User'
        }
    }]
});

groupSchema

var GroupSchema = Schema({
    name    : String, 
    created: {type:Date, default: Date.now},
    postedBy: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    }
});

虽然我可能在这个假设上是错误的,但我认为我已经向用户发表了评论,并且发表了评论,以仅仅以它应该的方式发布关系。 如果我错了,请纠正我。 我现在面临的主要问题是与我的小组发布关系。 目前,根本没有任何关系。 据我所知,就好像这个小组属于这个职位,而不是相反。 我能想到构建团队的唯一方式就是这样:

var GroupSchema = Schema({
        name    : String, 
        created: {type:Date, default: Date.now},
        postedBy: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User'
        }, 
        posts: [{
            title: String, 
            Url: String,
            comments: [{
                text: String
            }]
            etc....
        }]
    });

我在上面的结构中可以看到的唯一问题是该帖子将被要求成为一个组的一部分,我希望这个组成为可选组。 所以据我所知,这也行不通。

如果有什么建议可以提供关于这应该如何结构,这将帮助我很大。

先谢谢你。


I have a relatively simple question, however, I keep getting stuck, over and over again. I think this is the fourth time I have tried to re-structure my documents to try and get the desired result.

I want to create a database in mongoose with the following structure:

A User has many groups, posts and comments. The groups, posts and comments all belong to a User. A post can belong to many groups, or none. And a comment belongs to a certain post.

Here is what I have so far:

UserSchema

var userSchema = mongoose.Schema({

    local            : {
        email        : String,
        password     : String,
        username     : String,  
        created: {type:Date, default: Date.now} 

    }

});

PostSchema

var PostSchema   = new Schema({
    url: String, 
    highlighted: String, 
    comment: String, 
    image: String, 
    timeStamp: String, 
    description: String, 
    title: String,
    created: {type:Date, default: Date.now}, 
    private: Boolean, 
    favorite: Boolean, 
    readlater: Boolean, 
    postedBy: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    },
    comments: [{
        text: String,
        postedBy: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User'
        }
    }], 
    groups: [{
        name: String,
        postedBy: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User'
        }
    }]
});

groupSchema

var GroupSchema = Schema({
    name    : String, 
    created: {type:Date, default: Date.now},
    postedBy: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    }
});

Although I may be wrong on this assumption, I think I have the post to user and comment to post relationship just the way it should be. Please correct me if I am wrong. The main problem I am having right now is with my group to post relationship. Currently, there is no relationship at all. As far as I can, it is almost as if the group belongs to the post, instead of the other way around. The only other way I can think to structure the group would be like so:

var GroupSchema = Schema({
        name    : String, 
        created: {type:Date, default: Date.now},
        postedBy: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User'
        }, 
        posts: [{
            title: String, 
            Url: String,
            comments: [{
                text: String
            }]
            etc....
        }]
    });

The only problem I can see in the structure above is that the post will be required to be part of a group, which I want to be optional. So as far as I can tell, this will not work either.

If there is any advice you can provide in regards to how this should be structured, it would help me greatly.

Thank you in advance.


原文:https://stackoverflow.com/questions/33932925
2022-08-21 22:08

满意答案

尝试这个...

$('.demoClass').find('option[value="1"]').remove();

这利用了属性等于选择器

来自jquery


Try this...

$('.demoClass').find('option[value="1"]').remove();

This utilizes the Attribute Equals Selector

from jquery

相关问答

更多

jQuery:按值选择选项元素(jQuery: Selecting option elements by their value)

尝试这个... $('.demoClass').find('option[value="1"]').remove(); 这利用了属性等于选择器 Try this... $('.demoClass').find('option[value="1"]').remove(); This utilizes the Attribute Equals Selector

jQuery:选择属性大于值的所有元素(jQuery: Selecting all elements where attribute is greater than a value)

您可以使用.filter()的函数重载来执行此操作,如下所示: .filter(function() { return $(this).attr("attrName") > "someValue"; }) You can do this using the function overload of .filter(), like this: .filter(function() { return $(this).attr("attrName") > "someValue"; })

jQuery按值选择选项元素(jQuery select option elements by value)

这是一个最简单的解决方案,一个清晰的选择器: function select_option(i) { return $('span#span_id select option[value="' + i + '"]').html(); } Here's the simplest solution with a clear selector: function select_option(i) { return $('span#span_id select option[value="' +...

jQuery按名称选择元素(jQuery selecting elements by name)

另外,没有“名称”属性。 改用$(..).attr('name') 。 // Pozdrawiam;)/ [“问候”,in polish];) also, there's no "name" property. Use $(..).attr('name') instead. // Pozdrawiam ;) / ["greetings", in polish] ;)

使用jquery动态选择选项(Selecting dynamically an option using jquery)

几点观察 需要使用类adminBox监听输入,而不是所有复选框 使用checked属性查看是否已选中 adminBox是一个不是名字的类 使用.val()来设置select元素的值 它应该是 $("input.adminBox").change(function () { if (this.checked){ $("#role").val('admin'); } else { $("#role").val('sei'); } }); 演示:...

使用jquery选择选择选项不起作用(Selecting a select option using jquery not working)

当您调用.html()获取表格单元格的内容时,您将获得所有前导和尾随空白以及您感兴趣的文本; 另外,你应该使用.text()而不是.html()以防万一。 所以,只需使用replace删除空格: var s = $('#time').text().replace(/\s/g, '').split(':'); 演示: http : //jsfiddle.net/ambiguous/xmgAu/ When you call .html() to get the table cell's conten...

更改一个选择选项会改变许多其他选项(JavaScript,JQuery)(Changing one select option changes many others (JavaScript, JQuery))

$('select').change(function(){ var v = $(this).val(); $('select option[value="'+$(this).data('old-val')+'"]').prop('disabled', false); $(this).data('old-val',v); if(v != "0"){ $('select option[value="'+v+'"]').not(this).prop('d...

JQUery:在动态生成的选择选项列表中选择一个值(JQUery: selecting a value in a dynamically generated list of select options)

从注释/聊天中得到的澄清结果表明,加载器和setter函数是异步的,但它们是顺序调用的,因此当调用setter时,可能不会调用选项.... 您需要确保在加载器完成加载选项值后调用setter ...您可以使用回调机制来执行此操作。 阅读更多: 在这个答案中 From the clarification got from the comments/chats it turns out that loader and setter functions are async but they are ca...

使用jQuery在FireFox中从隐藏框中选择选项有问题(Having Issue on Selecting Option From Hidden Box in FireFox With jQuery)

您的HTML是错误的,因为HTML5不允许按钮内的交互式元素(例如选择)。您无法在按钮内添加选择。 在这里为你修好: 这是工作演示 HTML: <input type="radio" name="map" value="local" />Local <br /> <input type="radio" name="map" value="international" />International <br /> <div class="radio-div list-group" style="ma...

使用jquery选择所选元素的选项值(Selecting option value of selected element with jquery)

你快到了。 请给: var a = $(".select1 option:selected").attr("value"); 或者给这个: var a = $(".select1").val(); 您需要获取<select>的值。 You are almost there. Just give: var a = $(".select1 option:selected").attr("value"); Or give this: var a = $(".select1").val(); Yo...

相关文章

更多

MongoDB的体系结构_深入浅出MongoDB(三)

MongoDB和关系数据库逻辑结构关系对比,数据存储结构,MongoDB数据类型

快速了解MongoDB【基本概念与体系结构】

什么是MongoDB MongoDB is a general purpose, document-b ...

MongoDB: The Definitive Guide

How does MongoDB help you manage a huMONGOus amount ...

mongodb删除文档操作-使用shell和java操作mongodb文档

删除集合中的文档,使用命令 db.集合名称.remove({删除条件}),例如,db.c1.remov ...

mongodb添加集合文档操作-使用shell和java操作mongodb文档

查看当前数据库中所有的集合,使用命令 show collections 或使用show tables ...

对比下HBase, Memcached, MongoDB, Redis和Solr

转自:http://3834362.blog.51cto.com/3824362/1354092

Solr从Mongodb索引数据(上) 借助mongodb-jdbc和配置JdbcDataSource

Mongodb越来越流行,这边也用Mongodb去存储大量数据.但碰到一个问题,如何通过Solr从Mo ...

solr mongoDB 对比

1 solr 字段是写死的 2 solr优势是大块文本模糊查询,mongoDB还支持正则查询以及类似j ...

Mongodb与spring集成(2)------实体映射

spring-data-mongodb中的实体映射是通过 MongoMappingConverter这 ...

MongoDB集成Hadoop进行统计计算

MongoDB 本身可以做一些简单的统计工作,包括其内置的基于 Javascript 的 MapRed ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本