subdocument

Mongodb Subdocument Date Range Returns Wrong Results

我怕爱的太早我们不能终老 提交于 2019-12-04 19:45:33
first time in Stackoverflow. I'm trying to run a date range query on an array collection but Mongo Shell returning irrelevant documents witch doesn't match my criteria. It doesn't matter i'm doing the query trough PHP drivers, Doctrine Mongodb Query-builder or Mongo Shell. Here is my query: db.deals.find( { "total_sold.created_at": { $gt: new ISODate("2014-03-05T00:00:00Z"), $lt: new ISODate("2014-03-05T23:59:00Z") } }).limit(1).pretty() And here is the result: { "_id" : "1241412fb99a11a0bc70032a2cb6059b", "total_sold" : [ { "some_field": "value", "created_at" : ISODate("2014-02-13T15:48:35Z")

insert in subdocument with mongoDB

倾然丶 夕夏残阳落幕 提交于 2019-12-03 08:07:59
I have the following document in the collection: "_id" : "2", "workspace" : [{ "name" : "1", "widgets" : [ ] },{ "name" : "2", "widgets" : [ ] },{ "name" : "3", "widgets" : [ ] },{ "name" : "4", "widgets" : [ ] } ]} How can I insert {id: "1", blabla: "blabla"} in "widgets" for the "name" 3? In comparison to a previous answer which just inserts everything into the root of the document, here is a correct way to do this with positional operator : db.t.update({ "_id" : "2", "workspace.name" : "3" },{ $push: { 'workspace.$.widgets' : { id: "2", blabla: "blabla" } } }); 来源: https://stackoverflow.com

MongoDB remove an item from an array inside an array of objects

与世无争的帅哥 提交于 2019-12-01 03:56:00
I have a document that looks like this: { "_id" : ObjectId("56fea43a571332cc97e06d9c"), "sections" : [ { "_id" : ObjectId("56fea43a571332cc97e06d9e"), "registered" : [ "123", "e3d65a4e-2552-4995-ac5a-3c5180258d87" ] } ] } I'd like to remove the 'e3d65a4e-2552-4995-ac5a-3c5180258d87' in the registered array of only the specific section with the _id of '56fea43a571332cc97e06d9e' . My current attempt is something like this, but it just returns the original document unmodified. db.test.findOneAndUpdate( { $and: [ {'sections._id': ObjectId('56fea43a571332cc97e06d9e')}, {'sections.registered':

MongoDB remove an item from an array inside an array of objects

放肆的年华 提交于 2019-12-01 00:38:01
问题 I have a document that looks like this: { "_id" : ObjectId("56fea43a571332cc97e06d9c"), "sections" : [ { "_id" : ObjectId("56fea43a571332cc97e06d9e"), "registered" : [ "123", "e3d65a4e-2552-4995-ac5a-3c5180258d87" ] } ] } I'd like to remove the 'e3d65a4e-2552-4995-ac5a-3c5180258d87' in the registered array of only the specific section with the _id of '56fea43a571332cc97e06d9e' . My current attempt is something like this, but it just returns the original document unmodified. db.test

mongoose - ObjectId that references a Sub-Document

妖精的绣舞 提交于 2019-11-30 05:14:23
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); 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 model. You need to change your reference to the 'c' array. var C = new Schema({...}); var B = new Schema({c:

How to update MongoDB documents with arrays of sub-documents

旧时模样 提交于 2019-11-29 17:15:53
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 var studentSchema = new Schema({ name : { type: String, required: true }, grade : { type: Number,

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

若如初见. 提交于 2019-11-25 23:34:21
问题 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({ //