简体   繁体   中英

Mongodb - Create entry to a extisting collection field array

So I have a problem with my var array to add a new chapter, how would I go about this would I have to do this:

array.push({
            chapter: [
                {
                    id: 2,
                    title: 'adsf',
                    content: '',
                    authorNotes: 'asdf'
                }
            ]
        });

RiTest.ts

import * as mongoose from 'mongoose';

const Scheme = mongoose.Schema;

export const RiTestScheme = new Scheme({
    novelName: String,
    novelAuthor: String,
    novelCoverArt: String,
    novelTags: Array,
    chapters: [
        {
            id: Number,
            title: String,
            content: String,
            authorNotes: String
        }
    ]
});

export class RiTestController {
    public addChapter(callback: (data) => void) {
        var chapterInfoModel = mongoose.model('ChaptersTest', RiTestScheme);

        var array = [
        {
            chapter: [
                {
                    id: 0,
                    title: 'prolog',
                    content: 'conetntt is empty',
                    authorNotes: 'nothing is said by author'
                },
                {
                    id: 1,
                    title: 'making a sword',
                    content: 'mine craft end chapter',
                    authorNotes: 'nothing'
                }
            ]
        }
    ];

        let newChapterInfo = new chapterInfoModel(array);

        newChapterInfo.save((err, book) => {
            if (err) {
                return callback(err);
            } else if (!err) {
                return callback(book);
            }
        });
    }
}

This doesn't work, var array doesn't get saved into let newChapterInfo = new chapterInfoModel(array); what I am trying to do add another chapter to array.chapter but the array doesn't get recognized in the chapterInfoModel() how would I fix this array and add an item to the array to create a new entry into this existing collection

thank for taking your time to answer my question.

You are trying to insert array of document to your collections, That the reason its not inserting into your collection.

Document.prototype.save() will insert only one document to your collection depends on your definition. So to insert chapter here is the code below,

//array as Object
var array = {
    chapter: [
        {
            id: 0,
            title: 'prolog',
            content: 'conetntt is empty',
            authorNotes: 'nothing is said by author'
        },
        {
            id: 1,
            title: 'making a sword',
            content: 'mine craft end chapter',
            authorNotes: 'nothing'
        }
    ]
};

//Push to your chapter array
array.chapter.push({
    id: 2,
    title: 'adsf',
    content: '',
    authorNotes: 'asdf'
});

let newChapterInfo = new chapterInfoModel(array);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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