mongodb-query

How to apply condition on $lookup result in mongoDB?

て烟熏妆下的殇ゞ 提交于 2019-12-05 03:56:22
With the reference of my previous question, I have a question about $lookup with add some conditions. You may get enough reference about question from below link description. Photo: {_id: 1, photo_name: '1.jpg', photo_description: 'description 1', album_id: 1, flag:1 }, {_id: 2, photo_name: '2.jpg', photo_description: 'description 2', album_id: 1, flag:1 }, {_id: 3, photo_name: '3.jpg', photo_description: 'description 3', album_id: 1, flag:1 }, {_id: 4, photo_name: '4.jpg', photo_description: 'description 4', album_id: 2, flag:0 }, {_id: 5, photo_name: '5.jpg', photo_description: 'description

How to remove a document from the capped collection?

北慕城南 提交于 2019-12-05 03:08:44
When I try to remove a document in collection in MongoDB . It didn't remove because the collection is capped . My question is why? And is there a solution or other function can remove the document in this case? No You cannot delete documents from a capped collection. And there is no possible workaround. The only thing you can do is drop() the collection. Gaurav Radadiya You can not use remove function for capped collection. There are some restrictions on capped collections. Also you can refer this document for capped collection in mongoDB. 来源: https://stackoverflow.com/questions/33048282/how

mongoose to determine update-upsert is doing insert or update

穿精又带淫゛_ 提交于 2019-12-05 02:52:46
问题 I want to test if "upsert" option for updating is woking fine. So I "upsert" an object into mongodb twice with same key. However it didn't show inserted message. Was I missing something ? (mongodb : v2.6.3; mongoose : 3.8.15) Member.findOneAndRemove({user_id : 1}, function (err, doc) { if (!err) onsole.log(doc ? 'deleted' : 'not found'); }); // -> deleted, make sure user_id = 1 doesn't exist Member.update({user_id : 1}, {$set : {name:"name1"}}, {upsert : true, new : false}, // new : false, so

MongoDB find date range if overlap with other dates

流过昼夜 提交于 2019-12-05 02:46:32
问题 I have many documents with the schema shown below each containing (start date, enddate) as shown in the schema below. Is there a simple way to know before saving a new document if the new document startdate, enddate will overlap with previously saved documents startdate, enddate? Thanks { "title" : "", "owner" : "", "notes" : "", "startdate" : "", "enddate" : "" } Below is the only document currently saved: Document.(anonymous function) {_id: "FADnPAxRu4Ps5hkLz", title: "Round A", customerid:

MongoDB unwind multiple arrays

主宰稳场 提交于 2019-12-05 02:32:02
In mongodb there are documents in the following structure: { "_id" : ObjectId("52d017d4b60fb046cdaf4851"), "dates" : [ 1399518702000, 1399126333000, 1399209192000, 1399027545000 ], "dress_number" : "4", "name" : "J. Evans", "numbers" : [ "5982", "5983", "5984", "5985" ] } Is it possible unwind data from multiple arrays and get only paired elements from arrays: { "dates": "1399518702000", "numbers": "5982" }, { "dates": "1399126333000", "numbers": "5983" }, { "dates": "1399209192000", "numbers": "5984" }, { "dates": "1399027545000", "numbers": "5985" } From version 3.2 you can do it with

MongoDB - Logical OR when searching for words and phrases using full text search

a 夏天 提交于 2019-12-05 01:44:09
问题 I asked a related question previously, and as suggested by the poster there have created this new question as a follow up: MongoDB full text search - matching words and exact phrases I was having some problems with unexpected results when using the full text search functionality in MongoDB, specifically when searching for a mixture of words and phrases. Using this helpful example provided by the poster in the previous question... > db.test.drop() > db.test.insert({ "t" : "I'm on time, not

MongoDB: Query a key having space in its name

↘锁芯ラ 提交于 2019-12-05 01:32:40
I want to retrieve values of only certain keys from a MongoDB collection. But, the collection has some keys which have a 'space' in their name like: "Parent":{"key1": //some string, "key2": //some string, "key 3": //some string} I know this is a wrong approach as there shouldn't ideally be spaces in a key name but nevertheless how do I query this key? I am using Python and PyMongo. For normal keys I can do this: db.coll_name.find({"key": "India"}, {"_id": 0, "Parent.key1": 1, "Parent.key2": 1}) So how do I use the key "Parent['key 3']" in the second argument of the above query? Is there any

MongoDB: how to find documents ignoring case sensitive, accents and percent like logic (%)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 01:16:18
问题 I would like to make a search on a collection in my mongodb database. In my collection, I have documents with the field "name" can be values like: [i] "Palácio Guanabara", "Palácio da Cidade", "Festa Palácio", etc. When a user types a search like "pala" or "palá" or "Pala" or "PalÁ", all those itens in [i] must build the result set. I found that in MongoDB I could use regex in searches, like: { "name": { $regex: new Regex(".*pala.*", "i") } } Ok, this approach is case insensitive and use the

How do I unset all fields except a known set of fields?

谁都会走 提交于 2019-12-05 00:44:45
Suppose I have a single document in my mongo collection that looks like this: { "_id": 123, "field_to_prune": { "keep_field_1": "some value", "random_field_1": "some value", "keep_field_2": "some value", "random_field_2": "some value", "random_field_3": "some value" } } I want to prune that document to look like this: { "_id": 123, "field_to_prune": { "keep_field_1": "some value", "keep_field_2": "some value" } } However, my issue is that I don't know what the "random" field names are. In mongo, how would i $unset all fields except a couple of known fields? I can think of a couple of ways, but

remove documents with array field's size less than 3 in mongoDB

无人久伴 提交于 2019-12-05 00:33:00
问题 i have a mongoDB collection named col that has documents that look like this { { intField:123, strField:'hi', arrField:[1,2,3] }, { intField:12, strField:'hello', arrField:[1,2,3,4] }, { intField:125, strField:'hell', arrField:[1] } } Now i want to remove documents from collection col in which size of the array field is less than 2. So i wrote a query that looks like this db.col.remove({'arrField':{"$size":{"$lt":2}}}) Now this query doesnt do anything. i checked with db.col.find() and it