mongodb-query

How to return just the matched elements from a mongoDB array

好久不见. 提交于 2019-12-07 09:53:21
问题 I've been looking for this question one week and I can't understand why it still don't work... I have this object into my MongoDB database: { produc: [ { cod_prod: "0001", description: "Ordenador", price: 400, current_stock: 3, min_stock: 1, cod_zone: "08850" }, { cod_prod: "0002", description: "Secador", price: 30, current_stock: 10, min_stock: 2, cod_zone: "08870" }, { cod_prod: "0003", description: "Portatil", price: 500, current_stock: 8, min_stock: 4, cod_zone: "08860" }, { cod_prod:

Change an existing object in an array but still preserve key uniqueness

末鹿安然 提交于 2019-12-07 07:49:45
问题 I have a document: { 'profile_set' : [ { 'name' : 'nick', 'options' : 0 }, { 'name' : 'joe', 'options' : 2 }, { 'name' : 'burt', 'options' : 1 } ] } If I want to add new object to the profile_set only if the name of the object isn't already taken, regardless of the options , I can qualify my update with a query object that prevents the update if the name is already present in profile_set . In the shell: db.coll.update( {_id: id, 'profile_set.name': {$ne: 'nick'}}, {$push: {profile_set: {'name

What is the C# equivalent of push and root for MongoDB?

こ雲淡風輕ζ 提交于 2019-12-07 07:02:40
问题 I have a collection of people. I am trying to find the oldest person for each name. I am able to achieve that result using the mongoDB console command db.People.aggregate([ { '$sort': { 'Name': 1, 'Age': -1 } }, { '$group': { '_id': '$Name', 'docs': { '$push': '$$ROOT' }, } }, { '$project': { 'top_one': { '$slice': ['$docs', 1] } } } ]) What is the equivalent of this for the C# driver? I'm specifically having trouble with 'docs': { '$push': '$$ROOT' }, Here is my current C# query: collection

MongoDB: $push Multiple Objects, and $pop Multiple Objects in Single Update

大城市里の小女人 提交于 2019-12-07 05:55:27
问题 Imagine a MongoDB document with an array of 100 objects. And we want to keep the array length fixed at 100 . When a batch of new objects arrives (could be 1, 5, 10, etc.), we want to update the array with new objects, while removing an equal amount of old objects so the array length will stay fixed. I've opted to reading the array from MongoDB into my app, making some modifications, then using $set to update the array: var newData = [ { ... }, { ... }, { ... }, { ... }, { ... } ]; var oldData

Find all collections in mongodb with specific field

拈花ヽ惹草 提交于 2019-12-07 05:42:24
问题 There is more than 40 collections in database I am currently working on. One of the major key in all the collections is "account". I need to know all such collections where there is a field called "account". Is there a query to get or a js script which prints all such collections? In Oracle I was using : SELECT * FROM ALL_TAB_COLUMNS WHERE COLUMN_NAME LIKE 'account'; Any inputs is helpful. Thanks in advance. 回答1: The following mongo script will print out all collection names where at least

A pipeline stage specification object must contain exactly one field with php mongo aggregate

北城余情 提交于 2019-12-07 05:32:06
问题 I am trying to use aggregate with with project, match and sort but i am getting an exception ( MongoResultException to be exact) saying exception: A pipeline stage specification object must contain exactly one field. It works fine when I did not use sort and limit but I need them for this. The reason I am not using find() because I read somewhere that it can increase performance. Please help $query = array(.... //An actual query that works with find() $collection = $this->db->CollectionName;

Creating a mongodb query in Java with $or and $in

人走茶凉 提交于 2019-12-07 04:56:41
问题 I am trying to write a java code using mongodb api to create this mongodb query: { "$or": [{"prd" : {"$in" : ["1234", "0987"]}} , {"rsin" : "3228742"}]} Here's the code I am working with so far: QueryBuilder builder = new QueryBuilder(); if (builder == null) { builder = QueryBuilder.start(); } if (mongoKey.equals("prd")){ ArrayList<String> vals = new ArrayList<String>(); for (int i=0; i < prdList; i++){ vals.add(prdList.get(i)); } DBObject obj = new BasicDBObject (mongoKey, new BasicDBObject(

Represent object present at a position inside an array in mongoDB aggregation

倾然丶 夕夏残阳落幕 提交于 2019-12-07 04:15:25
In aggregation i am trying to project an object(which is an array) which is present at position 7 inside an array . For example i have fields as below { "$project": { "result": { "$cond": { "if": { "$eq": ["$coupon_type", 1] }, "then": ["$_id", "$coupon_type", "$nonce", "$total_coupons", "$amount", "$curr_ctr"], "else": ["$_id", "$coupon_type", "$nonce", "$total_coupons", "$amount", "$curr_ctr", "$coupon_codes", { indexes: conditions }] } } } }, { "$project": { obj: { $arrayElemAt: [ "$result", 7 ] } // here how to project indexes. } So now for coupon_type: 0 i will project my result as ["$_id

Mongodb limit array within aggregate query

断了今生、忘了曾经 提交于 2019-12-07 04:03:25
I'm attempting to write a query to return the top X terms across each category - e.g. top 5, top 10 etc. Each term has an associated category, and based up on some help from another stackoverflow question I've managed to get this: db.collection.aggregate([ { $group : { _id : { category: "$uri.category", term: "$uri.term", }, total: { $sum : 1 } } }, { $sort : { total : -1 } }, { $group : { _id : "$_id.category", terms: { $push: { term: "$_id.term", total: "$total" } } } } ]); The above query does work, and returns data that looks something like this: [ { category: "movies", terms: [ { term:

Find users who has not invited any user

二次信任 提交于 2019-12-07 04:02:56
问题 I want to find those users who has not invited any user by a single query (using aggregate). for Example : I have 4 users in DB { "_id" : ObjectId("581a18d41b6c5c752f11c87a"), "name": "aaa", "invitedBy": ObjectId("5808f53d28c14ee470856d8b") }, { "_id" : ObjectId("581a1a671b6c5c752f11c87b"), "name": "bbb", "invitedBy": ObjectId("581a18d41b6c5c752f11c87a") }, { "_id" : ObjectId("581a1a671b6c5c752f11c87c"), "name": "ccc", "invitedBy": ObjectId("581a18d41b6c5c752f11c87a"), }, { "_id" : ObjectId(