简体   繁体   English

带有ObjectId的Mongoose中的嵌套数组

[英]Nested Arrays in Mongoose with ObjectId

I am trying to record the id's of the posts that the user has voted on, and I am creating a schema for it like this: 我正在尝试记录用户已投票的帖子的ID,并且正在为它创建一个架构,如下所示:

var userSchema = new Schema({
    twittername: String,
    twitterID: Number,
    votedPosts: [{ObjectId : {votetype : Number}} ]
  });

The user gets created perfectly and I want to update the votedPosts attribute whenever the user votes on something so I call : 用户得到了完美的创建,并且每当用户对某项投票时,我都想更新votedPosts属性,因此我致电:

User.update({twitterID : req.body.userID} , { $push : {votedPosts : {postID : {votetype: newvotetype }}}} ,function (err, user, raw) {
    if (err){ 
        console.log(err);
    }
    else{
        console.log('user ' + user);
    }

});

Unfortunately the outcome is not as I described in my schema, I get this: 不幸的是结果不是我在架构中所描述的,我得到了:

  db.users.find().pretty()
    {
        "__v" : 0,
        "_id" : ObjectId("51bf4ef8dbda2f2e0c000001"),
        "twitterID" : 102016704,
        "twittername" : "gorkemyurt",
        "votedPosts" : [
            {
                "_id" : ObjectId("51bf5e48c3ffefe20c000002")
            }
        ]
    }

I am confused by the extra "_id" that mongoose adds in the votedPosts array.. Why cant ObjectId("51bf5e48c3ffefe20c000002") be the key of a key value pair? 猫鼬在投票的数组中添加了额外的“ _id”,这让我感到困惑。为什么ObjectId(“ 51bf5e48c3ffefe20c000002”)不能成为键值对的键?

I think your problem stems from your schema. 我认为您的问题源于您的架构。 You should try to define a schema in which the keys are not dynamically changing. 您应该尝试定义一个架构,在其中密钥不会动态更改。 MongoDB does not support queries on the keys of documents, just the values. MongoDB不支持对文档键的查询,仅支持值。

I think that using a dynamic "ObjectID" as the key in your schema above is leading to weirdness with Mongoose, and causing your issue. 我认为在上面的模式中使用动态的“ ObjectID”作为键会导致猫鼬怪异,并引起您的问题。 But, even if your query were propagated properly to MongoDB, it would still not produce the output you desire. 但是,即使您的查询已正确传播到MongoDB,它仍然不会产生您想要的输出。 The reason for this is that MongoDB will interpret "postID" as a String, regardless of whether you've defined some postID variable to hold a dynamic value. 这样做的原因是,无论您是否已定义一些postID变量来保存动态值,MongoDB都会将“ postID”解释为字符串。 This is the output if you run the query from the mongo shell, using a variable postID: 如果您使用变量postID从mongo shell运行查询,则输出为:

> var postID = 1234
> db.users.update( {twitterID : 102016704}, {$push : {votedPosts : {postID : {votetype: "newvotetype" }}}} )
{
    "__v" : 0,
    "_id" : ObjectId("51bf4ef8dbda2f2e0c000001"),
    "twitterID" : 102016704,
    "twittername" : "gorkemyurt",
    "votedPosts" : [
        {
            "postID" : {
                "votetype" : "newvotetype"
            }
        }
    ]
}

I would suggest that you use another schema. 我建议您使用其他架构。 The schema I've proposed below involves embedding a second schema into your existing schema . 我在下面提出的模式涉及将第二个模式嵌入到您现有的模式中 Try something like this: 尝试这样的事情:

var votedPost = new Schema({
    postID: ObjectId,
    votetype : Number 
  });

var userSchema = new Schema({
    twittername: String,
    twitterID: Number,
    votedPosts: [votedPost]
  });

This way, you can also query on the postID field, which MongoDB handles very nicely. 这样,您还可以查询MongoDB很好处理的postID字段。 Does that make sense? 那有意义吗? :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM