如何使用findByIdAndUpdate将objectId推送到对象的mongodb架构字段数组

时间:2017-08-05 07:06:50

标签: node.js mongodb mongoose

每次我打api我都会得到同样的错误 - 我已经尝试将值作为参数发送但是失败了。任何帮助,将不胜感激。当我使用<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/show_kitchen_key" android:textColor="@color/white" android:text="2592" android:visibility="invisible" android:background="@drawable/kitchen_key_background"/> </LinearLayout> 时,每次调用Web服务时它都会更新相同的值,但它确实有效,但不适用于$set

MongoError:该字段&#39; songId&#39;必须是数组,但在文档

中的类型为$push
objectId

我的播放列表架构代码: -

 {_id: ObjectId('59709590380026118c22dd61')}

我的歌曲模式代码

var PlaylistSchema = new mongoose.Schema({ 
  name: String,
  coverphoto: { type: String, default: '' },
  userId: {
    type: ObjectId,
    ref: 'User'
  },
  songId: [{ type: ObjectId, ref: 'Song' }],
  updated_at: { type: Date, default: Date.now },
});

我的Api代码

var mongoose = require('mongoose');
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;

var SongSchema = new mongoose.Schema({
  audioName:{type:String,default:''},
  title: String,
  // coverphoto: { type: String, default: '' },
  singer: { type: String, default: '' },
  movie: { type: String, default: '' },
  album: { type: String, default: '' },
  lyricist: { type: String, default: '' },
  actors: { type: String, default: '' },
  lyrics: { type: String, default: '' },
  genre: { type: String, default: 'Random' },
  duration: { type: String, default: '' },
  size: { type: String, default: '' },
  userId: {
    type: ObjectId,
    ref: 'User'
  },
  categoryId: {
    type: ObjectId,
    ref: 'Category'
  },    
  updated_at: { type: Date, default: Date.now },
});

module.exports = mongoose.model('Song', SongSchema);

0 个答案:

没有答案