mongodb-query

mongodb query by sub-field

孤街醉人 提交于 2020-01-13 07:26:44
问题 How to query all {"module" : "B"} ? The following query doesn't work: db.XXX.find({ "_id" : { "module" : "B" } }); Thanks a ton! There data looks like: { "_id" : {"module" : "A","date" : ISODate("2013-03-18T07:00:00Z")}, "value" : {"count" : 1.0} } { "_id" : {"module" : "B","date" : ISODate("2013-03-18T08:00:00Z")}, "value" : {"count" : 2.0} } 回答1: Try: db.XXX.find({ "_id.module" : "B" }); The difference is your original query would be trying to match on that entire subdocument (i.e. where

what is a collection scan in mongodb?

纵然是瞬间 提交于 2020-01-13 05:46:08
问题 I'm aware what a "full collection scan" is. But i'm a little unsure if the term "collection scan" applies to queries that use B-tree cursor. Do queries that use a cursor other than the basic cursor perform a collection scan? 回答1: The short answer is the two terms are the same, or rather there is only "full collection scan". If your query is using a B-tree cursor it is by definition not scanning the collection put is traversing the index in order to find the queried documents. A collection

Use of collation in mongodb $regex

拟墨画扇 提交于 2020-01-13 03:41:25
问题 Since v3.4 collations are available for find operations, especially as matches for diacritic characters are concerned. While a find query with a definite value ($eq opeartor or corresponding construct) will match letters and correspondent diacritics, the same is not true if a $regex is used in order to achieve a match on a partial search string (a 'LIKE'). Is there a to make the $regex query use the collation the same way than the $eq query? consider example collection testcoll: { "_id" :

Removing Latest document from Mongo DB in single query

一曲冷凌霜 提交于 2020-01-11 13:34:47
问题 I want to delete the latest document from my MongoDB in a single query. I have tried some commands but they doesn't seem to work. 回答1: The basic operation you are looking for is findOneAndDelete() in mongoose which is an atomic operation returning the "removed" document with the response. This only ever affects a single document and you get the "last" by applying a sort specification in the options. You basically then have two options for "last", either being by a field containing a BSON

Removing Latest document from Mongo DB in single query

别说谁变了你拦得住时间么 提交于 2020-01-11 13:34:25
问题 I want to delete the latest document from my MongoDB in a single query. I have tried some commands but they doesn't seem to work. 回答1: The basic operation you are looking for is findOneAndDelete() in mongoose which is an atomic operation returning the "removed" document with the response. This only ever affects a single document and you get the "last" by applying a sort specification in the options. You basically then have two options for "last", either being by a field containing a BSON

Push element in any position of array in the subdocument

余生颓废 提交于 2020-01-11 12:37:06
问题 In MongoDB 2.6 we can use $position (http://docs.mongodb.org/master/reference/operator/update/position/) modifier to specifies the location in the array during update of an array in a document . But I would like to insert in an array in a subdocument . Document schema: { subdoc: { array: ['0', '1', '2', '5', '6'] } } The following update pushes the elements in the end of array .. db.collection.update( { _id: tsId }, {$push: { 'subdoc.array': { $each:['3', '4'], $position:3 } }}); So, the

How to update a subdocument in mongodb

和自甴很熟 提交于 2020-01-11 11:26:20
问题 I know the question have been asked many times, but I can't figure out how to update a subdocument in mongo. Here's my Schema: // Schemas var ContactSchema = new mongoose.Schema({ first: String, last: String, mobile: String, home: String, office: String, email: String, company: String, description: String, keywords: [] }); var UserSchema = new mongoose.Schema({ email: { type: String, unique: true, required: true }, password: { type: String, required: true }, contacts: [ContactSchema] }); My

Get last documents with a distinct criteria

旧城冷巷雨未停 提交于 2020-01-11 10:15:31
问题 Situation I'm having trouble coming up with a good way to do a certain MongoDb query. First, here's what kind of query I want to do. Assume a simple database which logs entry and exit events (and possibly other actions, doesn't matter) by electronic card swipe. So there's a collection called swipelog with simple documents which look like this: { _id: ObjectId("524ab4790a4c0e402200052c") name: "John Doe", action: "entry", timestamp: ISODate("2013-10-01T1:32:12.112Z") } Now I want to list names

Return the last Document From a Lookup

只愿长相守 提交于 2020-01-11 07:46:06
问题 db.groups.aggregate([ { $lookup: { from: "posts", localField: "_id", foreignField: "group", as: "post" } } ]) I'm getting response for groups and all post like.. [{geoup1,[post's array]}, {group2,[post's array]}] If there is any post I just want last added post into post collection 回答1: You can either use $slice db.groups.aggregate([ { "$lookup": { "from": "posts", "localField": "_id", "foreignField": "group", "as": "post" }}, { "$addFields": { "post": { "$slice": ["$post", -1] } }} ]) Or

Mongodb returning multiple sub array results and excluding other returned results

十年热恋 提交于 2020-01-11 07:21:08
问题 I have the following collection with a node array: { "_id" : ObjectId("55acf6187d4c31475417fa62"), "node" : [ { "-id" : "29331496", "-uid" : "1899168", "-changeset" : "26313303", "-lat" : "-37.6104102", "-lon" : "144.9459817", "-timestamp" : "2014-10-25T03:36:51Z", "-user" : "heyitsstevo", "-visible" : "true", "-version" : "6" }, { "-id" : "29331497", "-uid" : "1899168", "-version" : "2", "-lon" : "144.9451088", "-timestamp" : "2014-10-25T03:36:51Z", "-user" : "heyitsstevo", "-visible" :