How to get key names from array in a collection which is start from specific name;
var regExp=/specific name of key/
var cur = db.collectionName.find();
cur.forEach(function(doc)
{{ Object.keys(doc).forEach(function(key)
{return key.match(regExp)})}}
var allKeys = {};
var regExp=/Alar/
db.collectionName.find().forEach(function(doc){Object.keys(doc).forEach(function(key){allKeys[key]=1})});
allKeys;
with above code output ;
{
"_id" : 1,
"id" : 1,
"type" : 1,
"Name" : 1,
"device_type" : 1,
"grammerData" : 1,
"code" : 1,
"Command" : 1,
"description" : 1,
"created_by" : 1,
"last_updated_time" : 1
}
now i want some script from which i can find those keys which start with specific name.
Its not working properly ,please help me out?
You can perform regex search in mongodb like the following:
db.collectionname.find({field:/abc/},function(err,data) {
//your code
}
This will search for all the documents that starts with abc. If you wanna search from more than one regex, you can use the $in operator to search for values inside an array:
db.collectionName.find({field:{$in:[/abc/,/what/]}},function(err,data){
//your code
}
This will return all the documents in which the field "field" starts with abc or what
Hope my answer was helpful.
Related
I'm new to MongoDB. I've an object below
{
"_id" : "ABCDEFGH1234",
"level" : 0.6,
"pumps" : [
{
"pumpNo" : 1
},
{
"pumpNo" : 2
}
]
}
And I just want to move level field to pumps array's objects like this
{
"_id" : "ABCDEFGH1234",
"pumps" : [
{
"pumpNo" : 1,
"level" : 0.6
},
{
"pumpNo" : 2,
"level" : 0.6
}
]
}
I've check on MongoDB doc in Aggregation section but didn't found anything. In SQL by JOIN or SUB Query I'm able to do but here it's No-SQL
Could you please help me with this? Thankyou
Try this on for size:
db.foo.aggregate([
// Run the existing pumps array through $map and for each
// item (the "in" clause), create a doc with the existing
// pumpNo and bring in the level field from doc. All "peer"
// fields to 'pumps' are addressable as $field.
// By $projecting to a same-named field (pumps), we effectively
// overwrite the old pumps array with the new.
{$project: {pumps: {$map: {
input: "$pumps",
as: "z",
in: {pumpNo:"$$z.pumpNo", level:"$level"}
}}
}}
]);
Strongly recommend you explore the power of $map, $reduce, $concatArrays, $slice, and other array functions that make MongoDB query language different from the more scalar-based approach in SQL.
I have the following CSV:
matchId, score, players.Name, players.Goals
2730319610399, 5-0, John, 3
When I use mongoimport on Studio 3T it is imported in the form I need because of the dot notation:
{
"matchId" : "2730319610399",
"score" : "5-0",
"players" : {
"Name" : "John",
"Goals" : "3"
}
}
My issue is that the csv actually has one more player that I want to add in this import. The array of "players" has two entries.
This is the actual CSV format:
matchId, score, players.Name, players.Goals, players.Name, players.Goals
2730319610399, 5-0, John, 3, Kyle, 2
But this does not work and I get an error of:
Every row will be parsed as one column.
The header contains a duplicate name "players.Name" in columns: [3, 5]
Is it possible to format the CSV so that I can add multiple values into the "players" array? I was thinking of naming it something like players[0].Name and players[1].Name
But that doesn't work because it creates two arrays: players[0] and players[1]
This is what I need the database structure to look like:
{
"matchId" : "2730319610399",
"score" : "5-0",
"players" : {
"Name" : "John",
"Goals" : "3"
},
{
"Name" : "Kyle",
"Goals" : "2"
}
}
Try with this:
matchId, score, players.Name, players.Goals
2730319610399, 5-0, John.Kyle, 3.2
then, use:
db.collection.find().snapshot().forEach(function (test) {
test.players.Name = test.players.Name.split('.');
db.whatevercollection.save(test);
});
db.collection.find().snapshot().forEach(function (test) {
test.players.Goals = test.players.Goals.split('.');
db.whatevercollection.save(test);
});
ok, so the best option would be to import a json file instead of a csv.
For example:
{ "matchId":"2730319610399","score":"5-0","players":[{"Name":"John","Goals":"3"},{"Name":"Kyle","Goals":"2"}]}
{ "matchId":"2830319610399","score":"1-0","players":[{"Name":"Mauri","Goals":"1"}]}
then use mongoimport as follows:
mongoimport --db=TestDB --collection=TestCol --type=json example_file.json
you should see something like this on Robo3T:
I have this simple database with one single collection and when I try a simple query with a field and value that exists il returns nothing.
one row of the database :
{
"title" : "Cupone Salice Salentino",
"sku" : 1000126,
"vendor" : "messapia-tesori-del-salento",
"image" : "",
"estimatedprice" : 21,
"finalprice" : 21,
"qty" : 1,
"category" : "Vins & alcools",
"status" : "fulfilled"
}
Code:
db.orders.find(); // this works
db.orders.find({qty : 2}); // this returns nothing
I think you have not gave whole document here.Because according to me "qty" is in array in your document object that's why.
I have collection "users" where username , password,questions are stored . "questions" is an array of documents . I would like to get all users with some questions i.e username , password and an array of questions (some part) How can i do that from console or java ?
Here i want to get username , password and first document of questions which is {question:"dawdaw",answered:0}
You can use slice like this
db.users.find({},{"questions":{$slice:1}})
Hope it will help
Use $elemMatch projection to get the desired result. The following find() operation queries for all documents where the $elemMatch projection returns only the first matching element of the questions array where the question field has a value of "dawdaw" and answered has 0 value:
db.users.find({},
{
"username": 1, "password": 1,
"questions": {
"$elemMatch": {
"question" : "dawdaw",
"answered" : 0
}
}
}
);
From the sample given, the operation returns the following document:
/* 0 */
{
"_id" : ObjectId("561a84ffaa233b38d803509a"),
"username" : "asd#mail.ru",
"password" : "asd",
"questions" : [
{
"question" : "dawdaw",
"answered" : 0
}
]
}
Assume we have the following collection, which I have a question about:
{
"_id" : 1,
"user_id" : 12345,
"items" : [
{
"item_id" : 1,
"value" : 21,
"status" : "active"
},
{
"item_id" : 2,
"value" : 22,
"status" : "active"
},
{
"item_id" : 3,
"value" : 23,
"status" : "active"
},
...
{
"item_id" : 1000,
"value" : 1001,
"status" : "active"
},
]
}
In a collection I have a lot of documents (as much as users in the system, at about 100K documents in collection). In every document I have around 1000 documents inside array "items"
The list of operations that will be used:
Read whole document once user logins to the system (rare operation).
Update a single document in a nested array items and set "value" and "status" almost on every "user click" (frequent operation)
db.items.update({_id : 1 , "items.item_id" : 1000} , {$set: {"items.$.value": 1000}})
Insert a new document to a collection with 1000 documents in nested array. This operation will be done on a new user registration (very rare operation)
The question is: Do I need to create a compound index like
db.items.createIndex( { "_id": 1, "items.item_id": 1 } )
to help the MongoDB to update certain document inside array or MongoDB does search in whole document no matter of compound index? Or maybe someone can propose a different schema for such a scenario?