subdocument

Returning subdocument array through Meteor / Mongo

家住魔仙堡 提交于 2020-01-05 15:20:25
问题 I'm having a little trouble returning and displaying tags that I'm adding to a subdocument. I have no problem adding tags, but want to put a label on the item for each tag. I simply can't find a resource that helps me return the items in an array within a subdocument. I think it's all the helper where I'm stuck - basically the syntactically correct way to write "Items.(this._id).itemTags.find();" :) Oh - and I've cut out a lot of the HTML and JS, but, yes, everything else is working fine. The

Returning subdocument array through Meteor / Mongo

六眼飞鱼酱① 提交于 2020-01-05 15:18:03
问题 I'm having a little trouble returning and displaying tags that I'm adding to a subdocument. I have no problem adding tags, but want to put a label on the item for each tag. I simply can't find a resource that helps me return the items in an array within a subdocument. I think it's all the helper where I'm stuck - basically the syntactically correct way to write "Items.(this._id).itemTags.find();" :) Oh - and I've cut out a lot of the HTML and JS, but, yes, everything else is working fine. The

MongoDB filter multi sub-documents

我只是一个虾纸丫 提交于 2020-01-02 15:05:39
问题 I Have a document structured like this in mongo DB, and I want to filter to show only active subdocuments: active cars and active fruits. { "name":"Andre", "fruits":[ { "active":true, "fruitname":"apple" },{ "active":false, "fruitname":"banana" } ], "cars":[ { "active":false, "carname":"ford" },{ "active":true, "carname":"GM" }, ] } this is my desired result. { "name":"Andre", "fruits":[ { "active":true, "fruitname":"apple" } ], "cars":[ { "active":true, "carname":"GM" }, ] } I've tried

Update a MongoDB subdocument when the parent document might not exist

隐身守侯 提交于 2019-12-30 09:32:35
问题 Here's my data, consisting of a books collection, with a books.reviews sub-collection. books = [{ _id: ObjectId("5558f40ad498c653748cf045"), title: "Widget XYZ", isbn: 1234567890, reviews: [ { _id: ObjectId("5558f40ad498c653748cf046"), userId: "01234", rating: 5, review: "Yay, this great!" }, { _id: ObjectId("5558f40ad498c653748cf047"), userId: "56789", rating: 3, review: "Meh, this okay." } ] }] In Node.js (using Mongoose), I need for users to be able to submit reviews via a form, sending

Find & return first matching subdocument from array (Meteor / Mongo)

自古美人都是妖i 提交于 2019-12-24 17:15:54
问题 how do I find and return the first subdocument in the 'tasks' array that matches completed: true? using findOne returns the entire document.. is there another function for returning a subdocument? { title: 'awebsite.com' company: 'a company' companyID: Random.id() category: 'website' starred: false timeline: { tasks: [ { name: 'task1' completed: true todos: [ {todo: 'something', completed: false, todoID: Random.id()} {todo: 'something', completed: false, todoID: Random.id()} {todo: 'something

Stop Mongoose from creating _id property for sub-document array items

て烟熏妆下的殇ゞ 提交于 2019-12-24 01:44:32
问题 If you have subdocument arrays, Mongoose automatically creates ids for each one. Example: { _id: "mainId" subDocArray: [ { _id: "unwantedId", field: "value" }, { _id: "unwantedId", field: "value" } ] } Is there a way to tell Mongoose to not create ids for objects within an array? 回答1: It's simple, you can define this in the subschema : var mongoose = require("mongoose"); var subSchema = mongoose.Schema({ //your subschema content },{ _id : false }); var schema = mongoose.Schema({ // schema

mongoose - ObjectId that references a Sub-Document

寵の児 提交于 2019-12-18 11:59:12
问题 Would it be possible for an ObjectId in ModelA to reference a sub-document in modelB ? var C = new Schema({...}); var B = new Schema({c: [C]}); var A = new Schema({c: { type: ObjectId, ref: 'ModelB.ModelC' }); var Model_A = mongoose.model('ModelA', A); var Model_B = mongoose.model('ModelB', B); var Model_C = mongoose.model('ModelC', C); 回答1: Yes it is possible, but you have a few options. Option 1: C as a Subdocument If you really want to use subdocuments, you don't need to create a separate

How to update MongoDB documents with arrays of sub-documents

和自甴很熟 提交于 2019-12-18 09:33:49
问题 I am working with a MongoDB database whose collections model classes , students , subjects , and [academic] performance . Below are the Mongoose based schema and models: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectId = Schema.Types.ObjectId; // SUBJECT collection var subjectSchema = new Schema({ name : { type: String, required: true }, category : { type: String, required: true } }); var Subject = mongoose.model('Subject', subjectSchema); // STUDENT collection

Fetching subdocuments with angular $http

纵饮孤独 提交于 2019-12-14 03:17:15
问题 Say you have nested collections a , b , and c , which follow the following map: {"collection":"a", "children":[{"collection":"b", "name":"bee", "children"[{"collection":"c","name":"cee"}]}]} And here is a1 , fetched from a MongoDb database with $http : {"title":"title a1", "id":"a1", "bee":[{"id":"b1"},{"id":"b2"}], "other_array":[{"foo":"bar"},{"foo":"baz"}]} Right now, in the bee array, we have only references ( id ). What we want is to keep following the map to update a1 , and replace

Mongoose sub document pre remove middleware not called

半世苍凉 提交于 2019-12-12 03:41:57
问题 I would prevent any sub document from being removed, thus I added an error to the pre('remove') middleware of each sub document Schema. When calling the .remove() function, it effectively calls the middleware. But when it is deleted without calling remove(), the middleware doesn't check if it has been removed. The cases where is matters is when I receive an object from a remote source, I'd like to perform all the integrity checks via mongoose middlewares to keep everything at the same place.