mongodb-query

MongoDB BasicDBObject vs Document in java

此生再无相见时 提交于 2019-12-04 06:41:44
I am using MongoDB v3.2.0 with Mongo Java Driver 3.0.4 version. I am using the BasicDBObject (deprecated) instead of using the Document in java, as I need to do many changes for converting to Document in my standalone java project. Can anyone tell me changing into Document, will there be any performance improvement in memory and large collection inserts and reads. Is there any way to improve my frequent write and read operations on MongoDB using java. Basic DBobject is not deprecated . The only deprecated part in the BasicDBobject is DBPointer class and few other methods like toString() and

How to Match on Joined Collections Using Laravel and MongoDB?

为君一笑 提交于 2019-12-04 06:24:39
问题 I have two collections "bookings" and "users". In "bookings" collection I have a field "user" . It is related to "users._id" . I have a booking listing page. In this listing page data from "bookings" and "users" are showing. In this listing page I have a text box to search invoice_number, payment_type, txid and usrEmail . I have written query to search invoice_number, payment_type, txid and it is working but I am stuck on usrEmail section. How can I combine with users and write search query

How to read a specific key-value pair from mongodb collection

懵懂的女人 提交于 2019-12-04 05:43:48
If I have a mongodb collection users like this: { "_id": 1, "name": { "first" : "John", "last" :"Backus" }, } How do I retrieve name.first from this without providing _id or any other reference. Also, is it possible that pulling just the `name^ can give me the array of embedded keys (first and last in this case)? How can that be done? db.users.find({"name.first"}) didn't work for me, I got a: SyntaxError "missing: after property id (shell):1 The first argument to find() is the query criteria whereas the second argument to the find() method is a projection, and it takes the form of a document

MongoDB merge related collection item count with other collection results

守給你的承諾、 提交于 2019-12-04 05:30:58
问题 I'm new to mongodb and trying figure out how to efficiently query on each item within a collection. I have projects collection & tasks collections //projects { _id: ObjectId(), name: String } //tasks { _id: ObjectId(), projectId: ObjectId(), //reference project id completed: Bool } I would like to get all projects and then count of completed and incomplete tasks of each project db.projects.find({})... //perhaps something similar in output [ { _id: ObjectId(), //projectId name: String

Mongo query aggregate with a conditional group and another field

空扰寡人 提交于 2019-12-04 05:22:34
问题 I followed this post by @Blakes-Seven Mongodb aggregate $group for multiple date ranges But I need to add an additional group by field "$User.Account" and I keep getting an error. When I take that out it works fine. What I'm trying to do and I'm pretty sure the below won't do it is find the top N users within each of the date ranges... { "message" : "the group aggregate field 'User' must be defined as an expression inside an object", "ok" : 0, "code" : 15951, "name" : "MongoError" } Any help

Populate specific fields in $lookup

£可爱£侵袭症+ 提交于 2019-12-04 05:07:48
问题 I am using aggregate to group and populate the result like below: { "$group": { "_id": "$userId", "projectId": { "$push": "$projectId" } } }, { "$lookup": { "from": "users", "localField": "_id", "foreignField": "_id", "as": "user" } }, { $unwind:"$user" }, { "$lookup": { "from": "projects", "localField": "projectId", "foreignField": "_id", "as": "projects" } } But i want to populate specific fields from that result For this I tried $project, But it combining projectId into one array and

Handling unwind for the non existing embedded document [duplicate]

不羁岁月 提交于 2019-12-04 05:07:01
问题 This question already has answers here : $unwind empty array (2 answers) Closed last year . I am performing aggregation on employees table to fetch some hostel details with projection like $query = ['_id' => new MongoDB\BSON\ObjectID($this->EmployeeId)]; $pipeline = array( ['$match' => $query], [ '$addFields'=> [ 'assigned_master_keys'=> [ '$cond'=> [ 'if'=> [ '$ne'=> [ [ '$type'=> '$assigned_master_keys' ], 'array' ] ], 'then'=> [], 'else'=> '$assigned_master_keys' ] ] ]], ['$unwind'=> '

How can we implement Pagination for Mongodb Collection using mongoTemplate

百般思念 提交于 2019-12-04 05:06:29
I'm a noob in mongoDb i need to implement Pagination for any specific Collection for instance say I have a Collection Foo and i have a Fucntion that returns all the records from the Foo collection public List<Foo> getFoo(){ } But i need to fetch records from the Foo by implementing pagination how can i achieve this by using mongoTemplate Spring data mongodb? For general pagination you can use the .skip() and .limit() modifiers on the Query object which you can pass in as arguments to your method: Query query = new Query(); query.addCriteria(Criteria.where("a").is("b")); query.skip(10); query

Mongodb sort documents by complex computed value

僤鯓⒐⒋嵵緔 提交于 2019-12-04 04:46:39
问题 items = collection.aggregate([ {"$match": {}}, {"$project": { 'temp_score': { "$add": ["$total_score", 100], }, 'temp_votes': { "$add": ["$total_votes", 20], }, 'weight': { "$divide": ["$temp_score", "$temp_votes"] } } } ]) The total_score and total_votes have stored in the document, I can get temp_score and temp_votes as expected, but can't get weight, any suggestion? 回答1: Your $temp_score and $temp_votes are not existing yet in your $divide . You can do another $project : db.user.aggregate(

MongoDB, Multiple count (with $exists)

て烟熏妆下的殇ゞ 提交于 2019-12-04 04:39:43
I have these 3 requests: db.mycollection.count({requestA:{$exists:true}}) db.mycollection.count({requestB:{$exists:true}}) db.mycollection.count({requestC:{$exists:true}}) I'd like to make only one request... So i tried the following: db.mycollection.aggregate( [ { $group: { '_id' : { user_id: '$user_id'}, requestA_count: { $sum: { $cond: [ {requestA:{'$exists':true}}, 1, 0 ] } }, requestB_count: { $sum: { $cond: [ {requestB:{'$exists':true}}, 1, 0 ] } }, requestC_count: { $sum: { $cond: [ {requestC:{'$exists':true}}, 1, 0 ] } }, } }, { $project: { _id: 0, user_id: '$_id.user_id', requestA