I have a json document as follows:
{
"_id" : ObjectId("553547d3864c23ae8837f8f2"),
"labels" : {
"id" : "1",
"name" : "Planet E",
"sublabels" : "Antidote (4)Community ProjectsGuilty PleasuresI Ner Zon SoundsPlanet E Communications, Inc.TWPENTY",
"contactinfo" : "Planet E Communications\nP.O. Box 27218\nDetroit, 48227, USA\n\np: 313.874.8729 \nf: 313.874.8732\n\nemail: info AT Planet-e DOT net\n",
"profile" : "Classic Techno label from Detroit, USA.\n[b]Label owner:[/b] [a=Carl Craig].\n",
"dataquality" : "Needs Vote",
"urls" : "http://planet-e.nethttp://planetecommunications.bandcamp.comhttp://www.discogs.com/user/planetedetroithttp://www.facebook.com/planetedetroithttp://www.flickr.com/photos/planetedetroithttp://plus.google.com/100841702106447505236http://myspace.com/planetecomhttp://myspace.com/planetedetroithttp://soundcloud.com/planetedetroithttp://twitter.com/planetedetroithttp://vimeo.com/user1265384http://www.youtube.com/user/planetedetroithttp://en.wikipedia.org/wiki/Planet_E_Communications"
}
}
When I use the following command
db.labels.find({name: "Planet E"})
I get the following result
What am I doing wrong?
You need to search for db.labels.find({ "labels.name": "Planet E" }). The name property is under the labels key.
Related
I am Very Beginner in mongodb.I have mongodb data with multiple array elements just update multiple data in single array element
"_id" : ObjectId("6217138f0607ea2e07e70b25"),
"phone_number" : "7645645676",
"email" : "test02#gmail.com",
"name" : "John",
"address" : "44th street Englan",
"devices" : [
{
"ime_number" : "123456789012345",
"device_name" : "Test456356",
"subscription_type" : "Yearly",
"validity" : 365,
"_id" : ObjectId("6217138f0607ea2e07e70b26"),
},
{
"ime_number" : "HHH555",
"device_name" : "Harry Potter",
"subscription_type" : "Monthly",
"validity" : 32,
"_id" : ObjectId("621714570607ea2e07e70b2b"),
}
]
Only This data
"ime_number" : "123456789012345",
"device_name" : "Test456356",
"subscription_type" : "Yearly",
"validity" : 365,
Changed data
"ime_number" : "888844442222",
"device_name" : "Remote Device",
"subscription_type" : "Monthly",
"validity" : 30,
Help me to solve this
I think you can use $ operator to do this.
The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array.
Now you have two options, to either update the individual fields (only the ones you want to) or update the whole object. For both of them you need $set :
Just pass the query as first argument of your update query and the second argument is your update clause.
Collection.update({'devices.device_name' : 'Test456356'}, {
{ '$set': { 'devices.$': {
"ime_number" : "888844442222",
"device_name" : "Remote Device",
"subscription_type" : "Monthly",
"validity" : 30,
} } }
});
Note: above updates all the elements found with the query. If you want to be specific you can use updateOne. Just replace update with updateOne and it will update the first matched document
I just want to get filed value where field is value of another filed.
ex: below I want to get valueOf("Brand")="Toshiba" where "Brand" is value of field mappingData.brand
{
"_id" : ObjectId("5640bdec1b988de0be317251"),
"Part number" : "PS460E-04NVX-NL",
"Brand" : "Toshiba",
"Quality" : "XYZ",
"Category" : "notebooks",
"Model Name" : "Satellite Pro 4600 PIII900",
"EAN" : "",
"Market Presence" : "N",
"Family" : "",
"Title" : "Toshiba Satellite Pro 4600 PIII900",
"mapping" : {
"brand" : "Brand",
"title" : "Title"
}
}
I am trying this following way but it is returning undefined.
db.products.find().limit( 5 ).forEach(function(myDoc) {
var b=myDoc.mappingData.brand;
print(myDoc.$$b);
})
Please help to resolve this issue.
Use the bracket notation to access the property of an object. In your case, this would be
db.products.find().limit(5).forEach(function(myDoc) {
var brand = myDoc.mappingData.brand,
brandVal = myDoc[brand];
print(brandVal);
})
I use mongoDB (version 3.0.2) for my application and if I will search for a User named "Hannes" than I get the following error above.
db.User.find() works without any problems.
Does anyone know what the problem is?
Thanks a lot!
db.User.find({$text:{$search:'Hannes'}})
Error: error: {
"$err" : "Unable to execute query: error processing query: ns=apoSoftDatabase.User limit=0 skip=0\nTree: TEXT : query=Hannes, language=, tag=NULL\nSort: {}\nProj: {}\n planner returned error: need exactly one text index for $text query",
"code" : 17007
}
[EDIT]
This is my document structure:
db.User.insert({ "_id" : ObjectId("5589929b887dc1fdb501cdbf"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "", "firstname" : "Claudia", "surname" : "Amreiter", "birthdate" : ISODate("1989-06-02T03:43:43.124Z"), "sex" : "FEMALE", "telephone" : "0664 / 342233223", "email" : "claudia.amreiter#aon.de", "username" : "claudia", "password" : "claudiapw", "address" : { "_id" : null, "street" : "strasse 21a", "postalCode" : 58441, "region" : "village", "country" : "Germany" }})
The "error" is because uou don't have a "text" index on your collection and you need one to use the $text operator in queries.
Create a text index
Your document may have fields like:
{
"a": "sam",
"b": "wise"
}
You can then create a "text index" over all or just a single field with something like:
db.collection.createIndex({ "a": "text", "b": "text" })
Then you can query with:
db.collection.find({ "$text": { "$search": "sam wise" } })
Or any other valid combination without problem.
I don't know the field name of your collection, however you can try this:
Try db.User.find({[field name]:'Hannes'})
I need to get all usernames from "followingList.username" and compare with
posts' usernames, if there any match need to add that one to an array.
Person Model
{
"_id" : ObjectId("554f20f5c90d3c7ed42303e1"),
"username" : "fatihyildizhan",
"followingList" : [
{
"_id" : ObjectId("55505b6ca515860cbcf7901d"),
"username" : "gumusluk",
"avatar" : "avatar.png"
},
{
"_id" : ObjectId("58505b6ca515860cbcf7901d"),
"username" : "yalikavak",
"avatar" : "avatar.png"
},
{
"_id" : ObjectId("58305b6ca515860cbcf7901d"),
"username" : "gumbet",
"avatar" : "avatar.png"
}
]
}
Post Model
{
"_id" : ObjectId("554f2df2a388R4b425b89833"),
"username" : "yalikavak",
"category" : "Summer",
"text" : "blue voyage with yacht"
},
{
"_id" : ObjectId("554f2df2a388P4b425b89833"),
"username" : "yalikavak",
"category" : "Winter",
"text" : "is coming ..."
},
{
"_id" : ObjectId("554f2df2a388K4b425b89833"),
"username" : "gumbet",
"category" : "Fall",
"text" : "there are many trees"
}
I try to get result code block as below but couldn't figure it out.
var filter = Builders<Post>.Filter.AnyEq("username", usernameList);
var result = collection.Find(filter).ToListAsync().Result;
Can you help me with this?
Thanks.
The logic is flipped, what you need is an $in query if I understand your use case correctly:
var filter = Builders<Post>.Filter.In("username", usernameList);
var result = collection.Find(filter).ToListAsync().Result;
In your case, username is a simple field and you want to match against a list of candidates. AnyEq is used to check that, from an embedded list of complex objects, at least one matches a criterion. That still translates to a simple query in MongoDB, but requires to 'reach into' the object which needs a more complicated syntax.
Since Orion 0.17.0 attribute type is no longer used as attribute "identification key". However, I have entities created with a pre-0.17.0 version that have attributes with the same name and different types. For example, the following entity, which have "ActivePower" duplicated:
> db.entities.findOne({"_id.type": "Regulator", "_id.id": "OUTSMART.RG_LAS_LLAMAS_01", "_id.servicePath": "/"})
{
"_id" : {
"type" : "Regulator",
"id" : "OUTSMART.RG_LAS_LLAMAS_01",
"servicePath" : "/"
},
"attrs" : [
{
"name" : "TimeInstant",
"value" : "2015-04-27T01:51:36.000000Z",
"type" : "urn:x-ogc:def:trs:IDAS:1.0:ISO8601",
"modDate" : 1430092302
},
{
"name" : "ActivePower",
"value" : "11778",
"type" : "urn:x-ogc:def:phenomenon:Outsmart:1.0:ActivePower",
"modDate" : 1430092302
},
{
"name" : "ReactivePower",
"value" : "8414",
"type" : "urn:x-ogc:def:phenomenon:Outsmart:1.0:ReactivePower",
"modDate" : 1430092302
},
{
"name" : "electricPotential",
"value" : "231",
"type" : "urn:x-ogc:def:phenomenon:IDAS:1.0:electricPotential",
"modDate" : 1430092302
},
{
"name" : "electricCurrent",
"value" : "20890",
"type" : "urn:x-ogc:def:phenomenon:IDAS:1.0:electricCurrent",
"modDate" : 1430092302
},
{
"name" : "Latitud",
"value" : "43.4716987609863",
"type" : "urn:x-ogc:def:phenomenon:IDAS:1.0:latitude",
"modDate" : 1414522843
},
{
"name" : "Longitud",
"value" : "-3.80692005157471",
"type" : "urn:x-ogc:def:phenomenon:IDAS:1.0:longitude",
"modDate" : 1401818472
},
{
"name" : "ActivePower",
"creDate" : 1393420396,
"value" : "11778.2",
"type" : "float",
"modDate" : 1430092302
}
],
"modDate" : 1430092302
}
How can I adapt that entity to work with Orion 0.17.0 and beyond?
The simplest solution is to edit the entity using mongo console, to remove all the "duplicated" attributes with the same name and leave only one. In the example above we have one ActivePower with type urn:x-ogc:def:phenomenon:Outsmart:1.0:ActivePower and other with type float. Let's assume we want to kept the first one.
First of all, stop Orion and take a backup of the database. If something gets wrong while you edit the entity, you could need that backup to go back the initial status and try again.
Next, run mongo console (let's assume that your DB is named "orion") and get the entity to modify using findOne() operation. Let's store it in the doc variable.
# mongo orion
> doc = db.entities.findOne({"_id.type": "Regulator", "_id.id": "OUTSMART.RG_LAS_LLAMAS_01", "_id.servicePath": "/"})
...
Now, identify the position within the attrs array of the attribute to remove, taking into account that the position of the first element in the vector is 0 (and not 1). Looking to the above example, the attribute to remove is the 7-th. Check that printing the attribute:
> doc.attrs[7]
{
"name" : "ActivePower",
"creDate" : 1393420396,
"value" : "11778.2",
"type" : "float",
"modDate" : 1430092302
}
Use splice() function to remove the attribute from doc, using as first parameter the position of the attribute and as second parameter 1. Print the doc value to check that it has been removed:
> doc.attrs.splice(7, 1)
...
> doc
Repeat the above operation as many time as you need to remove all duplicated (in the example case, there is only one duplicated). When you are done, save the new version of the entity in the DB:
> db.entities.save(doc)