mongodb-query

View more than 20 most recent documents in MongoDB Compass from Schema

醉酒当歌 提交于 2020-01-11 05:34:10
问题 I want to be able to edit some documents in my Mongo DB from compass, but a query only shows 20 of the most recent results. How can I view more than that 20? Or at least view the next 20? 回答1: From the docs ... You can specify a query filter in the query bar. If you click Options to expand the query bar, you also have access to the project, sort, skip, and limit options of the query. So, if you click on Options you can: Get more than 20 docs by changing the value of Limit from the default (

How can I get top n buckets for an aggregation and all other buckets combined into an “other” bucket?

自闭症网瘾萝莉.ら 提交于 2020-01-11 05:16:50
问题 Assume a collection with schema like as shown below: { "customer" : <unique-id-for-customer>, "purchase" : <number>, } Now, I want to get the the top 5 customers (by purchase-queantity) and the 6th bucket is "others" which combines all the purchase quantity from other customers. Basically, the output of aggregation should be like: { "_id" : "customer100", "purchasequantity" : 4000000 } { "_id" : "customer5", "purchasequantity" : 81800 } { "_id" : "customer4", "purchasequantity" : 40900 } { "

Cosmos Mongo API “In” Array expression issue

↘锁芯ラ 提交于 2020-01-11 04:11:26
问题 Are there issues with the IN expression? We are having issues where it doesn't find the results we are asking for even though it exists? { "SerialNumber": "V8073023", "_t": "DeviceEventDataEvent", "EventType": NumberInt(4), "isDeleted": { $in: [false,null] }, "EventNumber": 122 } Returns the document correctly, however when using IN { "SerialNumber": "V8073023", "_t": "DeviceEventDataEvent", "EventType": NumberInt(4), "isDeleted": { $in: [false,null] }, "EventNumber": { $in: [122] } } No data

Mongodb - aggregation $push if conditional

天涯浪子 提交于 2020-01-10 23:45:24
问题 I am trying to aggregate a batch of documents. There are two fields in the documents I would like to $push. However, lets say they are "_id" and "A" fields, I only want $push "_id" and "A" if "A" is $gt 0. I tried two approaches. First one. db.collection.aggregate([{ "$group":{ "field": { "$push": { "$cond":[ {"$gt":["$A", 0]}, {"id": "$_id", "A":"$A"}, null ] } }, "secondField":{"$push":"$B"} }]) But this will push a null value to "field" and I don't want it. Second one. db.collection

MongoDB get SubDocument

天大地大妈咪最大 提交于 2020-01-09 12:54:27
问题 I would like to retrieve a sub document from a document in MongoDB. I have the following document: { "_id" : "10000", "password" : "password1", "name" : "customer1", "enabled" : true, "channels" : [ { "id" : "10000-1", "name" : "cust1chan1", "enabled" : true }, { "id" : "10000-2", "name" : "cust1chan2", "enabled" : true } ] } The result I would like is: { "id" : "10000-1", "name" : "cust1chan1", "enabled" : true } However, the best I can do so far is using the following query: db.customer

MongoDB get SubDocument

只谈情不闲聊 提交于 2020-01-09 12:54:07
问题 I would like to retrieve a sub document from a document in MongoDB. I have the following document: { "_id" : "10000", "password" : "password1", "name" : "customer1", "enabled" : true, "channels" : [ { "id" : "10000-1", "name" : "cust1chan1", "enabled" : true }, { "id" : "10000-2", "name" : "cust1chan2", "enabled" : true } ] } The result I would like is: { "id" : "10000-1", "name" : "cust1chan1", "enabled" : true } However, the best I can do so far is using the following query: db.customer

Update multiple elements in an array in mongodb [duplicate]

只愿长相守 提交于 2020-01-09 08:04:23
问题 This question already has answers here : How to Update Multiple Array Elements in mongodb (14 answers) Closed 2 years ago . I have a document like following in my stocks collection in mongodb. { _id: 'xRMuRBhqRLgASyQyW', History: [ { orderId: '12032017001877', status: 'COMPLETED', }, { orderId: '22122017002450', status: 'PROCESSED', }, { orderId: '22122018002450', status: 'DEPOSIT', } ] } I want to iterate through all document in stocks collection and add a field flag: true if status is not

Update multiple elements in an array in mongodb [duplicate]

那年仲夏 提交于 2020-01-09 08:04:11
问题 This question already has answers here : How to Update Multiple Array Elements in mongodb (14 answers) Closed 2 years ago . I have a document like following in my stocks collection in mongodb. { _id: 'xRMuRBhqRLgASyQyW', History: [ { orderId: '12032017001877', status: 'COMPLETED', }, { orderId: '22122017002450', status: 'PROCESSED', }, { orderId: '22122018002450', status: 'DEPOSIT', } ] } I want to iterate through all document in stocks collection and add a field flag: true if status is not

How to continue insertion after duplicate key error using PyMongo

纵饮孤独 提交于 2020-01-09 03:50:06
问题 If I need to insert a document in MongoDB if it does not exist yet db_stock.update_one(document, {'$set': document}, upsert=True) .will do the job (feel free to correct me if I am wrong) But if I have a list of documents and want to insert them all what would be a best way of doing it? There is a single-record version of this question but I need an en mass version of it, so it's different. Let me reword my question. I have millions of documents, few of which can be already stored. How do I

Using stored JavaScript functions in the Aggregation pipeline, MapReduce or runCommand

爱⌒轻易说出口 提交于 2020-01-09 02:39:04
问题 Is there a way to use a user-defined function saved as db.system.js.save(...) in pipeline or mapreduce? 回答1: Any function you save to system.js is available for usage by "JavaScript" processing statements such as the $where operator and mapReduce and can be referenced by the _id value is was asssigned. db.system.js.save({ "_id": "squareThis", "value": function(a) { return a*a } }) And some data inserted to "sample" collection: { "_id" : ObjectId("55aafd2bacbed38e06f9eccf"), "a" : 1 } { "_id"