mongodb-query

Return only specific fields from projection array sub-document

百般思念 提交于 2019-12-20 12:42:28
问题 I'm looking at MongoDB's documentation on the $ and $elemMatch projections. I'm trying to figure out how to return only a subset of a projection array's fields, but I cannot seem to figure it out. Related posts: I am not trying to perform a $slice from mongodb aggregation framework - Fetch first document's field of the nested array. Nor am I trying to flatten the sub-document from Return only array value in mongo projection because I still want some fields from the top document. Say I have

How to get mongo command results in to a flat file

杀马特。学长 韩版系。学妹 提交于 2019-12-20 08:27:31
问题 How do I export the results of a MongoDB command to a flat file For example, If I am to get db.collectionname.find() into a flat file. I tried db.collectionname.find() >> "test.txt" doesnt seem to work. 回答1: you can try the following from the command line mongo 127.0.0.1/db --eval "var c = db.collection.find(); while(c.hasNext()) {printjson(c.next())}" >> test.txt assuming you have a database called 'db' running on localhost and a collection called 'collection' this will export all records

MongoDB query with conditional group by statement

故事扮演 提交于 2019-12-20 07:56:28
问题 I need to export customer records from database of mongoDB. Exported customer records should not have duplicated values. "firstName+lastName+code" is the key to DE-duped the record and If there are two records present in database with same key then I need to give preference to source field with value other than email. customer ( id,firstName,lastName,code,source ) collection is this. If there are record 3 records with same unique key and 3 different sources then i need to choose only one

How to use aggregation function mongo db-query

做~自己de王妃 提交于 2019-12-20 07:29:08
问题 I am new in MongoDB and I would like to use the aggregation function where I want to check type == topic and get the following output Expected output [ { conceptName : 59d98cfd1c5edc24e4024d00 totalCount : 2 }, { conceptName : 59d98cfd1c5edc24e4024d03 totalCount : 1 } ] Sample input db.GroupContents { "_id" : "5a0948bb1c5edc7a5000521a", "type" : "topic", "groupID" : "5a0948bb1c5edc7a5000521a", "pedagogyID" : "59d98cfa1c5edc24e40249a3", } Sample input db.PedagogyNodes { "_id" :

How to define a circle for a mongo db schema?

99封情书 提交于 2019-12-20 07:13:26
问题 I have the folowing code to define a schema: var mongoose=require('mongoose'); var Schema=mongoose.Schema; var PostSchema=new Schema({ location:{type:Array,required:false,select:true} }) PostSchema.index({location:'2dsphere'}); module.exports=mongoose.model('Post',PostSchema); I want to make location field to be seen like a circle(ex:location:{loc:[latitude,longitude],radius:radius})..I want to make a query to find circles form my db which are intersecting with a polygon.Thanks for any help :

Count Elements SubDocument that match a given criterion

吃可爱长大的小学妹 提交于 2019-12-20 07:12:43
问题 I have the following document structure in mongodb { "_id" : "123", "first_name" : "Lorem", "last_name" : "Ipsum", "conversations" : { "personal" : [ { "last_message" : "Hello bar", "last_read" : 1474456404 }, { "last_message" : "Hello foo", "last_read" : 1474456404 }, ... ], "group" : [ { "last_message" : "Hello Everyone", "last_read" : null } ... ] } } I want to count the number of conversations from the sub arrays, personal and group where the last_read is null, for a given user. Please

MongoDB querying from Aggregation to Group

拜拜、爱过 提交于 2019-12-20 06:44:45
问题 I have this following query in MongoDB: var subquery = {"shipdate" : { "$gte" : 19960101, "$lt" : 19960401 } }; var eachsupp = db.lineitems.aggregate([ { $match : subquery }, { $project : { "_id" : 0, "revenue" : {$multiply : ["$extendedprice", {$subtract : [1, "$discount"] }] }, "supplier_no" : "$partsupp.supplier.suppkey", "name" : "$partsupp.supplier.name", "address" : "$partsupp.supplier.address", "phone" : "$partsupp.supplier.phone" }}, { $group : { _id : "$supplier_no", total_revenue :

Meteor mongodb $inc with update

冷暖自知 提交于 2019-12-20 06:36:51
问题 I have this mongo collection and vars: items:{ type_one: 0, type_two: 0 } var valueOne = 1; var nameItem = type_one; I try to update a value, but not work. I tried this: Collection.update({createdBy: user_id}, {$inc: { items.$.nameItem: valueOne}} ); Collection.update({createdBy: user_id}, {$inc: { "items.$.nameItem": valueOne}} ); Collection.update({createdBy: user_id}, {$inc: { "items."+nameItem: valueOne}} ); Collection.update({createdBy: user_id}, {$inc: { "items."+nameItem: 1}} ); var

MongoDB: Checking if nested array contains sub-array

可紊 提交于 2019-12-20 06:32:11
问题 I have a use case where the database is modelled like this: name: XYZ gradeCards: [{ id: 1234, // id of the report card comments: ['GOOD','NICE','WOW'] }, { id: 2345, comments: ['GOOD','NICE TRY'] }] Now, I have a query that I would like to query the schema as follows: I would be given a list of ids and values. For example: the given list is as follows: [{ id: 1234, comments: ['GOOD','NICE'] },{ id: 2345, comments: ['GOOD'] }] In short the ID should be matching and the comments should be a

What would be the MongoDB C# driver equivalent of the following query using the array update operator $[<identifier>]

China☆狼群 提交于 2019-12-20 06:28:15
问题 From https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/#up.S[%3Cidentifier%3E] Given the following collection { "_id" : 1, "grades" : [ { "grade" : 80, "mean" : 75, "std" : 6 }, { "grade" : 85, "mean" : 90, "std" : 4 }, { "grade" : 85, "mean" : 85, "std" : 6 } ] } { "_id" : 2, "grades" : [ { "grade" : 90, "mean" : 75, "std" : 6 }, { "grade" : 87, "mean" : 90, "std" : 3 }, { "grade" : 85, "mean" : 85, "std" : 4 } ] } and query db.students2.update( { }, { $set: {