Menu
  • HOME
  • TAGS

How could I get the matched nested items in array

Tag: mongodb

How could I get the matched nested items in array

I want to return the matched items in nested array.

For example, I want to filter out the records that contain "A428 ","A429 " in their items

How could I get it ?

Query

pipeline_work = [
  { '$match': 'records.items': '$in': ["A428 ","A429 "])}
]
  cur = db[source_collection].runCommand('aggregate',pipeline: pipeline_work , allowDiskUse: true)

Sample Document

  {
    "_id": "0007db2dac8d6482ec60c228b700c3ec",
    "records": [
      {
        "APPL_DATE": new Date("1996-03-19T08:00:00+0800"),
        "FUNC_DATE": new Date("1996-02-27T08:00:00+0800"),
        "items": [
          "A428 ",
          "     ",
          "     "
        ]
      },
      {
        "APPL_DATE": new Date("1996-03-19T08:00:00+0800"),
        "FUNC_DATE": new Date("1996-02-27T08:00:00+0800"),
        "items": [
          "A429 ",
          "     ",
          "     "
        ]
      },
      {
        "APPL_DATE": new Date("1996-04-15T08:00:00+0800"),
        "FUNC_DATE": new Date("1996-03-18T08:00:00+0800"),
        "items": [
          "A180 ",
          "     ",
          "     "
        ]
      }]

Expected result

  "_id": "0007db2dac8d6482ec60c228b700c3ec",
  "records": [
    {
      "APPL_DATE": new Date("1996-03-19T08:00:00+0800"),
      "FUNC_DATE": new Date("1996-02-27T08:00:00+0800"),
      "items": [
        "A428 ",
        "     ",
        "     "
      ]
    },
    {
      "APPL_DATE": new Date("1996-03-19T08:00:00+0800"),
      "FUNC_DATE": new Date("1996-02-27T08:00:00+0800"),
      "items": [
        "A429 ",
        "     ",
        "     "
      ]
    }]

Failed with {"errmsg":"exception: A pipeline stage specification object must contain exactly one field.","code":16435,"ok":0}

pipeline_work = [
  {
    "$unwind": "$records",
    "$match": {
      "records.items": {
        "$in": ["A428 ", "A429 "]
      }
    },
    "$group": {
      "_id": "$_id",
      "records": {
        "$push": "$records"
      }
    }
  }, {
    '$limit': 1
  }
];

Best How To :

You should first $unwind records and then match elements like following:

db.collection.aggregate({
    "$unwind": "$records"
}, {
    "$match": {
    "records.items": {
        "$in": ["A428 ", "A429 "]
    }
    }
}, {
    "$group": {
    "_id": "$_id",
    "records": {
        "$push": "$records"
    }
    }
}).pretty()

DB relationship: implementing a conversation

mongodb,meteor

It all depends on usage patterns. First, you normalize: 1 conversation has many messages, 1 message belongs to 1 conversation. That means you've got a 1-to-many (1:M) relationship between conversations and messages. In a 1:M relationship, the SQL standard is to assign the "1" as a foreign key to each...

How to add a subdocument in a mongoose schema

javascript,node.js,mongodb,mongoose

Since the address field is not required by Mongoose when you retrieve the Model from the database the field just won't be defined. Thus you won't be able to add an address field. You should check to see if it exists if(member.address === undefined){ member.address.push(new_address); } else{ member.address = [new_address];...

Mongodb query documents where nested array is equal or subset of fixed array

c#,arrays,mongodb,subset,mongodb-csharp

Use mongo Set Operator using $setIsSubset in aggregation you will get your result, check following query : db.collectionName.aggregate({ "$project": { "Name": 1, "Tags": 1, "match": { "$setIsSubset": ["$Tags", ["A", "B", "C"]] //check Tags is subset of given array in your case array is ["A","B","C"] } } }, { "$match": {...

Data not visible in mongoDB when inserted from PyMongo

python,mongodb,pymongo

Your code "works' but I think you may have copied-and-pasted something wrong. In particular, you're swapping the use of find() and find_one(). The enter_data() method calls insert() without specifying _id so the driver will invent one for you. That _id ends up being an ObjectId similar to this: { "_id"...

MongoJS - No Error On Unique Index

mongodb,mongojs

This seems to be a "buggy" response from mongojs which likely results from it's implementation of returning the "inserted" object as a response. The "core" driver does not do this but would either return a "ok" response or an error: var MongoClient = require('mongodb').MongoClient; MongoClient.connect('mongodb://localhost/test', function(err,db) { var collection =...

Getting failed to load c++ bson extension error using Mongodb and Node.js

javascript,node.js,mongodb

Since you're using the mongojs module, you're going to have to connect to the database using the following method db = mongo("127.0.0.1:27017/"+db, collections); ...

How to use a variable as an Object Key [MongoDB] [duplicate]

node.js,mongodb

JavaScript has no nice notation for creating an object where the keys are variables: var $push_query = {}; $push_query[name] = item; ... {"$push": $push_query} ... ...

Get json values from key-value pair without passing keys in meteor

javascript,json,mongodb,meteor

JS Template.test.helpers({ itemsArray: function(){ // This would normally be the result of your Mongo query var itemsArray = [ { "_id": "SXTJBs7QLXoyMFGpK", "Brand": "Nike", "Material": "Cooton", "Price": "67484", "ComboId": "y23", "Color": "White", "LaunchDate": "08/02/2015", "DiscountActiveDate": "08/03/2015", "DiscountInactiveDate": "08/04/2015", "Category": "Sport", "ProductSubCategory": "trackpant", "Status": "Pending", "TemplateID": "557fc7d06ecb48d38a67a380" }, { "_id": "IGJHihljiUYG6787y",...

Use JSON file to insert data in database

javascript,json,mongodb,meteor,data

Simple use underscores _.extend function. Like this: var newProfile = _.extend( JSON.parse(Assets.getText('test.json')), {user: id} ) Profiles.insert(newProfile) ...

What are some patterns I can look at for database implementations in JavaScript?

javascript,node.js,mongodb

That's a pretty wide question, partly opinion based. This question should be closed, but I still want to give you some advice. There once was the Active Record pattern, which has been proven to be pretty difficult to maintain. The solution was the DAO pattern, but this adds a lot...

mongodb bind ip won't work unless set to 0.0.0.0

mongodb

You can bind mongod only to one IP, with 0.0.0.0 being the alias for "listen on all available network interfaces". So either use bind_ip=127.0.0.1 to listen to the loop back interface or bind_ip=<someIP> to listen to that IP only or bind_ip=0.0.0.0 to listen to all available IPs on the system....

mongodb populate method not working

node.js,mongodb,model,populate,auto-populate

When you create an instance of the Post model, you need to assign the _id from the user as an ObjectId, not a string: var ObjectId = require('mongoose').Types.ObjectId; Post.create({ text: 'farheen123', created_by: new ObjectId('5587bb520462367a17f242d2') }, function(err, post) { if(err) console.log("Farheen has got error"+err); else console.log(post); }); ...

AngularJS factory dependencies

javascript,html,angularjs,mongodb

DOCS Change your factory to service as you are using this factory return an object or primitive type not bind with this app.services('articleFactory', ['$q', '$http', function ($q, $http){ this.getAllArticles = function (){ var deferred = $q.defer(), httpPromise = $http.get ('/entries'); httpPromise.success (function (data){ deferred.resolve (data); }) .error (function (err){ console.log...

Query with filter builder on nested array using MongoDB C# driver

c#,mongodb,mongodb-query,mongodb-csharp,mongodb-csharp-2.0

The query you need to perform uses the $elemMatch query operator. So, this query using a lambda expression var findFluent = collection.Find(f => f.Bars.Any(fb => fb.BarId == "123")); Is equivalent to this query using the FilterDefinitionBuilder: var findFluent = collection.Find(Builders<Foo>.Filter.ElemMatch( foo => foo.Bars, foobar => foobar.BarId == "123")); ...

Async await usage for MongoDB repository

c#,mongodb,asynchronous,parallel-processing,async-await

When you call an async method you should await the returned task, which you can only do in an async method, etc. Awaiting the task makes sure you continue execution only after the operation completed, otherwise the operation and the code after it would run concurrently. So your code should...

MongoDB Java Driver 3.0 MapReduce

mongodb,mongodb-java

Yes, this is the expected behavior. Since the MapReduceIterable is a fluent interface, there must be some way to signal the driver that it's time to actually do the map-reduce, and currently the only way to do that is to start iterating. If you really don't need the results, and...

mongodb aggregate unwind array and no array

arrays,mongodb,aggregation-framework

This basically does what you want with some help from $cond and $ifNull: db.collection.aggregate([ { "$project": { "x": 1, "y": { "$cond": [ { "$ifNull": [ "$y.0", null] }, "$y", { "$map": { "input": ["A"], "as": "el", "in": "$y" }} ] } }} ]) So those first conditions work out...

How could I get the matched nested items in array

mongodb

You should first $unwind records and then match elements like following: db.collection.aggregate({ "$unwind": "$records" }, { "$match": { "records.items": { "$in": ["A428 ", "A429 "] } } }, { "$group": { "_id": "$_id", "records": { "$push": "$records" } } }).pretty() ...

Is there a built-in function to get all unique values in an array field, across all records?

arrays,node.js,mongodb,mongoose,schema

You can use the distinct function to get the unique values across all category array fields of all documents: Article.distinct('category', function(err, categories) { // categories is an array of the unique category values }); Put an index on category for best performance....

How can i insert current date in robomongo?

java,mongodb,robomongo

db.yourcollection.insert({date_field_name:new Date()})

Cassandra data model to store embedded documents

mongodb,database-design,cassandra

If your document nesting level is not too deep, you can use User Defined Types from C* 2.1. I personally suggest to rethink your schema into more flat form like: create table profiles ( name text, name2 text, email text, username text, ts timestamp, primary key (name,name2) // compound primary...

What does "new BsonValue[]{x,y,z} do?

mongodb,mongodb-csharp

BsonDocument provides flexible way to represent JSON/BSON in C#. Creating BsonDocument is similar to creating JSON objects. Simple document new BsonDocument("name", "Joe") creates JSON { "name" : "Joe" } More complex document new BsonDocument { {"Name", "Joe"}, { "Books", new BsonArray(new[] { new BsonDocument("Name", "Book1"), new BsonDocument("Name", "Book2") }) }...

Use $or operator in @Query annotation in spring data mongodb repository

java,spring,mongodb,spring-data-mongodb

Per MongoDB reference for $or, your Query should be @Query("{'$or':[ {'type':?0}, {'name':?1} ]}") you'll need to give pass type and name params....

Mongoose : update collection with previous data

node.js,mongodb,mongoose

First of all the schema you posted is off, should be { album : "album1", pictures: [ 1.jpg, 2.jpg, 3.jpg ] } note "pictures" is an array not an object. You can add to the array with //SAVE DB AlbumPhoto.update({album:'album1'}, {$push: {pictures: "new.jpg"}}, function(err, data){ if(err){ console.log(err); } }) ...

Get matching elements in mongodb array

php,mongodb,aggregation,pipeline

It's true that the "standard projection" operations available to MongoDB methods such as .find() will only return at most a "single matching element" from the array to that is queried by either the positional $ operator form in the "query" portion or the $elemMatch in the "projection" portion. In order...

Meteor/MongoDB limiting the result

mongodb,meteor

Meteor's collection API is somewhat different from that of the mongo API. find takes up to two parameters: a selector object, and an options object. options allows you to specify such things as sort, skip, limit and fields, in addition to the meteor-specific reactive and transform.

Do you get the same performance using index prefixes?

performance,mongodb,indexing

A "compound index" which is the correct term for your "link" does not create any performance problems on "read" ( since writing new entries is obviously more information ) than an index just on the single field used in the query. With one exception. If you use a "multi-Key" index...

How to get node.js to connect to mongolab using mongoose

database,node.js,mongodb,mongoose,mongolab

Try using db = mongoose.connect(uri); instead of db = mongoose.createConnection(uri); ...

Unclean shutdown with MongoDB results in corrupted mongod.lock file

node.js,mongodb

Client connections to MongoDB do not affect the mongod.lock file. The error message relates to your DB server aka the mongod process shutting down uncleanly. The solution for that is to always shutdown your DB process cleanly. Unclean shutdown should be an exceptional situation which happens out of your control....

Query not working - MongoDB

mongodb

Looks like you've missed some brackets before $elemMatch: db.items.find( { "$or": [ {"f": {$elemMatch: {"t": "ry", "v": {$gt: 1980}}}}, {"f": {$elemMatch: {"t": "g", "v": {$in: ["Drama"]}}}} ] } ) ...

Why the query scans 8x times the documents it finds - MongoDB

mongodb

It's not "documents" it's "Objects" as is stated, but you would not be the first person to not fully understand the .explain() output. Put simply, you have as part of your index an "array" element (actually the maximum "two" allowed), which means that your index is what we call "MultiKey"....

Query an array of embedded documents in mongodb

mongodb,embedded-documents

You can push the item into the array with the following command: db.mycollection.update({ _id: "zinfandel", "last_search.engine": { $nin: ["notwellknownengine.com"] } }, { $push: { "last_search": { "engine" : "notwellknownengine.com", "query" : "stackoveflow.com" } } }); ...

Are the changes done by db command in MongoDB permanent and how?

mongodb,mongodb-query

Yes, Changes made to data in MongoDB shell are permanent but for configuration parameters, they will last only till next restart of instance if they are not specified in config file. MongoDB runs with default values if config file is not specified. You can specify config file as, mongod --config...

Live Preview Iframe

javascript,node.js,mongodb,iframe

I wouldn't build it entirely on Node.js I would rather use the power of the modern client-side Javascript and move some logic there with persisting some of the data coming from the server. This way I can lower the number of requests and make my script more flexible. Two libraries...

How to add new items to an array in MongoDB

arrays,node.js,mongodb

$set is not an array update operation. The $set operator replaces the value of a field with the specified value. You just want to use $push by itself, as in .update({_id: id}, {$push: {name: item}}) You can't interpolate object property names in raw object declarations, so if you want to...

paging subdocument in mongodb subdocument

mongodb,mongodb-query

Though this is possible to do with some real wrangling you would be best off changing the document structure to "flatten" the array entries into a single array. The main reason for this is "updates" which are not atomically supported by MongoDB with respect to updating the "inner" array due...

Incorrect response to mapReduce query in mongo-db

mongodb,mapreduce

Your problem here is that you have missed one of the core concepts of how mapReduce works. The relevant documentation that explains this is found here: MongoDB can invoke the reduce function more than once for the same key. In this case, the previous output from the reduce function for...

Compare Array with Collection-Array containing Objects

node.js,mongodb,mongoose,schema

You can try this: objectModel.find({ 'members.user_id' : {'$in' : ['asdf123lkd', 'asdf1223']} }, function(err, data) { console.log(err,data); }) ...

c# mongodb driver groupby

c#,mongodb,mongodb-csharp

I think the driver just doesn't implement the Linq GroupBy method. Use aggregate framework instead. You can find good examples here - MongoDB Aggregation Framework Examples in C# You don't need to link any new libraries the support for the aggregation framework goes with MongoDB C# driver. Hope it helps!...

Inserting a variable in MongoDB specifying _id field

python,mongodb,pymongo

Insert only accepts a final document or an array of documents, and an optional object which contains additional options for the collection. db.collection.insert( <document or array of documents>, { // options writeConcern: <document>, ordered: <boolean> } ) You may want to add the _id to the document in advance, but...

How to overwrite object Id's in Mongo db while creating an App in Sails

mongodb,sails-mongo

Attention: Mongo id should be unique as possible in order to scale well. The default ObjectId is consist of a timestamp, machine ID, process ID and a random incrementing value. Leaving it with only the latter would make it collision prone. However, sometimes you badly want to prettify the never-ending...

Creating index while updating the documents

mongodb,indexing

The amount of time it takes to add the index would depend on your hardware, but with 20206 records a simple index as you describe shouldn't take very long for most hardware. Queries fully covered by the index (i.e. where you specify A and B, or just A, but not...

push item in sub document

mongodb,mongodb-query

You need the positional $ operator in your update portion of the statement: db.getCollection('forms').update( { "_id" : ObjectId("557e8c93a6df1a22041e0879"), "Questions._id" : ObjectId("557e8c9fa6df1a22041e087b") }, { "$push" : { "Questions.$.DataSource" : { "_id" : ObjectId("557e8e5ea6df1a27b403ff6b"), "CreationDate" : ISODate("2015-06-15T08:35:42.923Z"), "IsActive" : true, "Text" : "op", "Value" : "op" } } } ) It identifies...

Mongotemplate - Query ObjectId according to greater than (gt) or less than (lt) operator

mongodb,spring-data,objectid

So after searching for an hour, I have found the solution - i had to look at this post which is not in java but in node.js. Querying a MongoDB based on Mongo ID in a node.js app Thankfully, the language is close to java so I saw that you...

Convert string to ISODate in MongoDB

javascript,python,mongodb,date

If you can be assured of the format of the input date string AND you are just trying to get a count of unique YYYYMMDD, then just $project the substring and group on it: var data = [ { "name": "buzz", "d1": "2015-06-16T17:50:30.081Z"}, { "name": "matt", "d1": "2018-06-16T17:50:30.081Z"}, { "name":...

MongoDB explain() cursor field

mongodb

explain() output changed in 3.0. "Theirs" is <3.0 output.

using MongoDB aggregate count subdata

mongodb,aggregate

please try the below query : db.collection.aggregate( [ { "$unwind": "$data.order_goods" }, { "$group" : { "_id" : "$data.order_goods.category", "total": {"$sum":1} } }, { "$project" : { "category" : "$_id" , total : 1 } ] ); ...

Mongoose population in instance methods

node.js,mongodb,mongoose,mongoose-populate

In Mongoose instance methods, this is the document instance the method is being called on, so you can do this as: MainSchema.methods = { retrieveChilds: function(callback) { this.deepPopulate('childs.subject.data', callback); }, }; Then to call it: main.retrieveChilds(function(err, _main) { // _main is the same doc instance as main and is populated....

How to check if multiple documents exist

mongodb,mongodb-query

You are looking for the $in-operator. db.collection.find({ id: { $in: { [ 1, 3 ] } }); This will get you any documents where the id-field (different from the special _id field) is 1 or 3. When you only want the values of the id field and not the whole...

Find Mongoid geospacial circles that contain a point

mongodb,mongoid,geospatial

Modelling a "circle" is a valid approach by specifying a "centre" and a "radius", but finding things "within the circle" is generally straightforward if not immediately obvious: Service.collection.aggregate([ # Get the distance from the current location { "$geoNear" => { "near" => { "type" => "Point", "coordinates" => [ 1,...