Mongoose 更新/架构设计

发布于 2024-12-27 13:33:36 字数 2381 浏览 3 评论 0原文

我正在尝试更新猫鼬模型。遇到一些困难。这就是我的架构现在的样子。

我希望用户拥有一个包含许多歌曲的库。这些歌曲可以位于多个用户库中。并且还可以位于多个播放列表中,用户可以拥有多个播放列表。

var LibrarySchema = new Schema({
        user: Schema.ObjectId
    });


    var PlaylistSchema = new Schema({
        title: String,
        description: String,
        //songs: [SongsSchema],
        user: Schema.ObjectId
    });


    var SongsSchema = new Schema({
        name: String,
        artist: String,
        album: String,
        time: Number,
        url: String,
        gid: String,
        location: String,
        playlist: [{playlist_id:Schema.ObjectId, position: Number}],
        library: [Schema.ObjectId]
    });

现在我想做一些事情,比如检查 url 是否已经存在于歌曲架构中(url 是一个索引)。如果那首歌不存在,我想添加它的所有值。但是,如果歌曲存在,我想附加播放列表并将其放置在库中。

例如,这是我现在正在尝试的查询:

Song.update({url: s.url, "playlist.playlist_id": playlistId}, {$set: {name: s.name,    artist: s.artist, album: s.album, time: s.time, url: s.url}, $addToSet: {playlist: {playlist_id: playlistId, position: s.position}, library: libId}},{upsert: true}, function(err, data) {});

这是存储的数据库的示例:

{ "name" : "Kanye West - All Of The Lights ft. Rihanna, Kid Cudi", "artist" : "unknown",   "album" : "unknown", "time" : 328, "url" : "HAfFfqiYLp0", "_id" : ObjectId("4f127923ce0de70000000009"), "library" : [ ObjectId("4f1203af23c98cfab1000006") ],  "playlist" : [
{
    "playlist_id" : ObjectId("4f127923ce0de70000000007"),
    "position" : "1"
}
] }
{ "name" : "Kanye West - Heartless", "artist" : "unknown", "album" : "unknown", "time" : 221, "url" : "Co0tTeuUVhU", "_id" : ObjectId("4f127923ce0de7000000000a"), "library" : [ ObjectId("4f1203af23c98cfab1000006") ], "playlist" : [
{
    "playlist_id" : ObjectId("4f127923ce0de70000000007"),
    "position" : "2"
}
] }
{ "name" : "Kanye West - Stronger", "artist" : "unknown", "album" : "unknown", "time" :  267, "url" : "PsO6ZnUZI0g", "_id" : ObjectId("4f127923ce0de7000000000b"), "library" : [  ObjectId("4f1203af23c98cfab1000006") ], "playlist" : [
{
    "playlist_id" : ObjectId("4f127923ce0de70000000007"),
    "position" : "3"
}
] }

现在我想做的是,如果歌曲的 url 不在数据库中,则应该将其与所有信息一起添加。如果不是,那么它将添加到库(每个用户都有一个)库 id 除非它已经包含该值。

这效果相当不错。我现在遇到的问题是,当它找到我希望它添加到播放列表的 url 时,当前播放列表的 playlistId 除非它已经在数组中,并且如果是,并且传递了一个新的位置值(用户可以更改播放列表的顺序)它应该改变位置的值。

也许我没有以正确的方式建模。任何提示都会很棒!

I am trying to update a mongoose model. Having some difficulites. This is how my schema looks like right now.

I want a user to have a library which consists of many songs. Those songs can be in multiple users libraries. And can also be in multiple playlists of which a user can have several.

var LibrarySchema = new Schema({
        user: Schema.ObjectId
    });


    var PlaylistSchema = new Schema({
        title: String,
        description: String,
        //songs: [SongsSchema],
        user: Schema.ObjectId
    });


    var SongsSchema = new Schema({
        name: String,
        artist: String,
        album: String,
        time: Number,
        url: String,
        gid: String,
        location: String,
        playlist: [{playlist_id:Schema.ObjectId, position: Number}],
        library: [Schema.ObjectId]
    });

Now I want to do something like check if the url already exists in the song schema (url is an index). If that song does not exist I want to add it with all the values. However, if the song exists I want to append the playlist and position its in and the library.

For example here is the query I am trying now:

Song.update({url: s.url, "playlist.playlist_id": playlistId}, {$set: {name: s.name,    artist: s.artist, album: s.album, time: s.time, url: s.url}, $addToSet: {playlist: {playlist_id: playlistId, position: s.position}, library: libId}},{upsert: true}, function(err, data) {});

And this is a sample of the database stored:

{ "name" : "Kanye West - All Of The Lights ft. Rihanna, Kid Cudi", "artist" : "unknown",   "album" : "unknown", "time" : 328, "url" : "HAfFfqiYLp0", "_id" : ObjectId("4f127923ce0de70000000009"), "library" : [ ObjectId("4f1203af23c98cfab1000006") ],  "playlist" : [
{
    "playlist_id" : ObjectId("4f127923ce0de70000000007"),
    "position" : "1"
}
] }
{ "name" : "Kanye West - Heartless", "artist" : "unknown", "album" : "unknown", "time" : 221, "url" : "Co0tTeuUVhU", "_id" : ObjectId("4f127923ce0de7000000000a"), "library" : [ ObjectId("4f1203af23c98cfab1000006") ], "playlist" : [
{
    "playlist_id" : ObjectId("4f127923ce0de70000000007"),
    "position" : "2"
}
] }
{ "name" : "Kanye West - Stronger", "artist" : "unknown", "album" : "unknown", "time" :  267, "url" : "PsO6ZnUZI0g", "_id" : ObjectId("4f127923ce0de7000000000b"), "library" : [  ObjectId("4f1203af23c98cfab1000006") ], "playlist" : [
{
    "playlist_id" : ObjectId("4f127923ce0de70000000007"),
    "position" : "3"
}
] }

Now what I am trying to do is if the url for the song is not in the database it should add it with all the information. If it is not then it will add to the library (which each user has one of) the library id UNLESS it already contains that value.

That works pretty good. The problem I am having right now is when it finds the url I want it to add to playlist the playlistId of the current playlist UNLESS it is already in the array and if it is and was passed a new value for position (user can change the order of a playlist) it should change the value of the position.

Perhaps I am not modelling it in a proper way. Any tips would be awesome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰葑 2025-01-03 13:33:36

MongooseJS 包含一个叫做 DBRef 的东西。我认为您想使用它,而不是自己处理所有引用。

要插入不存在的内容,请先进行查找并在需要时插入,或者使用 upsert< /a>.不确定 Mongoose 是否支持更新插入...

MongooseJS contains something called DBRef. I think you want to use that, instead of handling all the references yourself.

For inserting if something is not there, either do a lookup first and insert if needed, or use an upsert. Not sure if Mongoose supports Upserts though...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文