subdocument

How to 'skip' and 'include' subdocuments with mongoose query

Deadly 提交于 2019-12-12 03:32:55
问题 Let's say I have this structure Account { Id, Name, Password, Details { LastSignedIn, CreatedDate Address { City, Country } } } Details subdocument belongs to Account , Address subdocument belongs to Details With mongoose can I say to retrieve the particular subdocument or skip the other one with findqueries? Something like: //will return in result 'Account' with only 'DetailsSubdocument' included Account.findOne({name:'user'}, include: {'Account.DetailsSubdocument'}) //will return in result

Subtract subdocuments from collection in aggregate pipeline

若如初见. 提交于 2019-12-11 23:13:11
问题 I'm trying to retrieve a document from a mongodb and remove some objects from an array based on a condition ( deleted != null ). Here is a simplified example of the item I'm targeting: { "_id" : ObjectId("54ec9cac83a214491d2110f4"), "name" : "my_images", "images" : [ { "ext" : "jpeg", "type" : "image/jpeg", "_id" : ObjectId("54f2311026b0cb289ed04188"), "deleted" : null, "date_added" : ISODate("2015-02-28T21:20:16.961Z"), }, { "ext" : "jpeg", "type" : "image/jpeg", "_id" : ObjectId(

getting the sub-document with the document in mongoose.findOne

ぐ巨炮叔叔 提交于 2019-12-11 19:05:58
问题 First, my code is working well, but i don t know if doing the right thing. I have a parent model Units with this schema { info: {...}, chapters: {_id: string, ...} } the chapter schema is like this: { info: {...}, content: string } now in the client if i am editing the a chapter, and want to put the changes to the database, i do it like this Units.findOne({'chapters._id': chapter_id}) //this query will return the parent unit .then(unit=>{ //is there a way to have the chapter with unit here??

Trouble updating a specific subdocument with MongoDB

邮差的信 提交于 2019-12-11 13:22:34
问题 My document looks like: { _id: ObjectId("52d317d7b5c4960000587cd4"), txid: "7e621eeb02874ab039a8566fd36f4591e65eca65313875221842c53de6907d6c", vin: [ { _id: ObjectId("52d317d7b5c4960000587ce9"), meta_address: "321", meta_amount: 50, sequence: 4294967295, txid: "6749762ae220c10705556799dcec9bb6a54a7b881eb4b961323a3363b00db518", vout: 0 }, { _id: ObjectId("52d317d7b5c4960000587ce8"), sequence: 4294967295, txid: "c04c413576307737f3ad48efe5d509ebc883e1d04822b3a2eccf6a80a4482932", vout: 0 }, {

How can I update the sub document field from the sub document array based on the query?

别等时光非礼了梦想. 提交于 2019-12-11 09:09:09
问题 { "_id": ObjectId("552cd780e4b042752e540df5"), "source": [ { "bookid": ObjectId("552cd77e31456e192df6ad8e"), "isActive": false }, { "bookid": ObjectId("552cd77e31456e192df6ad8a"), "isActive": true } ] } I need to update the above document , where the condition is the source.bookid is ObjectId("552cd77e31456e192df6ad8e") and update that particular document field isActive as false . So, the resulting output I need as { "_id": ObjectId("552cd780e4b042752e540df5"), "source": [ { "bookid":

editing subdocments N-N relationship in mongodb

佐手、 提交于 2019-12-10 14:54:26
问题 I have an application where an article can be linked to multiple platforms . Article contains a list of platforms and platforms also contains a list of articles. For more detailed information please look at this stackoverflow question that I asked a few months ago. https://stackoverflow.com/a/40377383/5770147 The question was on how to create an article and implement the N-N relationship between article and platform. I have Creating article and Deleting the article setup so that the lists

MongoDB indexes on subdocuments not being used based on syntax of query

我怕爱的太早我们不能终老 提交于 2019-12-10 10:44:21
问题 I'm using Meteor with MongoDB with a collection of documents like this: {a: 'a1', b: 'b1', c: { d: 'd1', e: 'e1' } } I initially created an index like this: collection._ensureIndex({'c.d': 1}); And ran queries like this: collection.find({c: {d: 'd1'}) . These ran extremely slowly and when I debugged with explain(), I realized they were not using the index. OTOH, if I ran a query like this: collection.find({'c.d': 'd1'}) , then Mongo would use the index. I've now changed the index to index the

insert in subdocument with mongoDB

僤鯓⒐⒋嵵緔 提交于 2019-12-09 06:59:33
问题 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? 回答1: 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" :

Mongodb Subdocument Date Range Returns Wrong Results

时间秒杀一切 提交于 2019-12-06 12:18:59
问题 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" :

MongoDB filter multi sub-documents

早过忘川 提交于 2019-12-06 04:29:10
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 Aggregate but when any cars or any fruits active, it's return nothing. m_object.aggregate([ { $match : {